CS5200PR1 – Setup Development Environment Solved

35.00 $

Category:

Description

5/5 - (1 vote)

Introduction

For this course we will be using the Java programming language to implement Spring Boot based Web applications hosted on Amazon Web Services (AWS). This assignment will walk you through setting up the local development environment and deploying a simple Hello World Web application on AWS. We will be expanding this application as the semester progresses. This document is ​ also available onlin​ e.​

Learning Objectives

By the end of this assignment you should be able to

  • Configure a local Java development environment
  • Create a simple Spring Boot Web application
  • Configure a remote Tomcat server running on AWS
  • Deploy a Spring Boot Web application to AWS

Setup Java JDK 8

We will be using the latest version of Jav​            a throughout the semester. Navigate to Oracle’s Java Development Kit (JDK) 8 download website

 

http://www.oracle.com/technetwork/java/javase/downloads/index.html

 

Download and install the JDK for your particular operating system. Once the JDK has installed, verify you have the right version of Java installed. From your terminal or console, type the following

 

> java -version java version “1.8.0_77”

Java(TM) SE Runtime Environment (build 1.8.0_77-b03)

 

On macOS you can find out where Java is installed by typing the following on the terminal:

 

> which java

/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home//bin/java

 

On Windows, Java should install in Program Files/Java​

 

Verify environment variable JAVA_HOME​ points to the Java installation folder. From your terminal or console, type the following

 

> echo $JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/

 

The above path might be different in your particular operating system. If JAVA_HOME​ is not set, set it for your particular operating system and add Java’s bin directory to the PATH​ environment variable. On macOS, or other Unix like OSs, add JAVA_HOME​ as an environment variable in your ~/.bash_profile​ .​ The .bash_profile​ is a hidden configuration file in your home directory (~​).​ If you have a new machine it might not yet exist, so you might need to create the file (touch​ )​ . From your terminal, type the following

 

> cd ~

> touch .bash_profile

> edit .bash_profile

 

This will open .bash_profile​ in your system text editor. Add the following line at the end your file​

 

export JAVA_HOME=/my/path/to/jdk/jdk1.8.0_77.jdk/Contents/Home/ export PATH=$JAVA_HOME/bin:$PATH

 

Where the path /my/path/to/jdk/​ will be different for your particular machine.​

 

On Windows, set up environment variables in the E​ nvironment Variables Control Panel

  1. Select S​ tart​, select C​ ontrol Panel​. Double click S​ ystem​, and select the A​ dvanced​ tab
  2. Click E​ nvironment Variables
  3. In the E​ dit System Variable (or N​ ew System Variable​) window, specify the value of the JAVA_HOME​ environment variable
  4. Edit the PATH​ environment variable and add ​ %JAVA_HOME%\bin​

Setup Maven

Maven is a Java dependency package manager. It simplifies managing the lifecycle of projects such as downloading libraries, compiling, running automated tests, and packaging projects for deployment. Download maven from

 

http://maven.apache.org/download.cgi

 

  1. Download the latest ZIP file version, e.g., apache-maven-3.5.2-bin.zi​ p.​
  2. On macOS, unzip to /urs/local/apache/maven/​ . On Windows, unzip to ​ /Program Files/apache/maven/​ .​
  3. Create environment variables M2_HOME​ and MAVEN_HOME​ to refer to the paths where you unzipped maven. Latest Maven documentation only mentions M2_HOME​ ,​ but older tools might still use the older environment variable MAVEN_HOME​ .​
  4. Add M2_HOME/bin​ and MAVEN_HOME/bin​ to your PATH​ environment variable so you can execute maven from the terminal or console.
  5. In a new terminal or console verify maven is installed by typing the following

 

> mvn –version

Apache Maven 3.5.2 (r01de14724cdef16f02da; 2013-02-19 08:51:28-0500)

Maven home: /usr/local/apache-maven-3.0.5

Java version: 1.8.0_77, vendor: Oracle Corporation

Java home: /my/path/to/jdk/jdk1.8.0_77.jdk/Contents/Home/jre

Default locale: en_US, platform encoding: UTF-8

OS name: “mac os x”, version: “10.12.6”, family: “mac”

 

Your output might vary slightly

Create a Spring Boot Web Maven Project

Spring Boot is a preconfigured Sprin​ g framework that allows creating Spring based Java applications with minimal effort. Spring Boot bootstraps a Spring application based on a heavily preconfigured set of default assumptions, hence the name Spring Boot. The assumptions Spring Boot makes address 80% of the needs of typical applications. Additional configuration would be needed when exploring the other 20%.

Setup Spring Tool Suite (STS)

Spring Tool Suite (STS) is an E​ clipse based IDE that has been optimized for working with Sprin​ g Boot projects.​ Download STS for your operating system from

 

https://spring.io/tools/sts/all

 

unzip it, run the installer, and follow the installation instructions. On macOS, drag the S​ TS application icon to the Applications​ folder. On Windows, unzip to Program Files/spring​ . Run STS and accept the default configuration.​

Create a Spring Starter Project

Using STS, create a Spring Boot project. From the File menu, select New and then Spring Starter Project. Configure the project based on the name of the course, the semester term, and your last name. For instance, consider the following course, semester and last name

 

  • Course: cs5200
  • Semester: Spring 2018
  • Last name: Annunziato

 

use the following configuration values:

 

  • Name and Artifact: cs5200-spring2018-annunziato
  • Group and Package: edu.northeastern.cs5200
  • Packaging: War

 

Add W​ eb​, J​ PA​, and A​ pache Derby as a project dependencies and click finish. Find the path of the project folder by right clicking on the project, selecting Properties and Resources on the left, and then Location on the right. Make a note of the path since we’ll need it in a later step.

Create an HTML Index File

Once the project is created we’ll add a simple index.html​ in the webapp​ folder by right clicking the webapp​ folder, selecting N​ ew​, O​ ther​, W​ eb​, H​ TML File​, N​ ext​, and type the name of the file: index.html​ .​ Use the following content for the new index.html​ file, replacing my name with YOUR NAME:​

 

<!DOCTYPE html>

<html>

<head>

<meta charset=”UTF-8″>

<title>Insert title here</title>

</head>

<body>

<h1>Hello Jose Annunziato!</h1>

</body>

</html>

Test Web Application Locally

Spring Boot allows to easily run a Web application locally by running a boilerplate class file that bootstraps the Spring framework and serves as the entry point of the server application. In STS, navigate to your application entry point, e.g., src/main/java/Cs5200Spring2018AnnunziatoApplication.java​        and run it as a Java Application. To run it, right click on the class, select R​ un As​, and then J​ ava Application​. This will start a local embedded Tomcat server that listens at port 8080. To test it, open a browser and point it to localhost:8080​ .​ Verify the browser displays ​Hello Jose Annunziato!, but instead of my name, YOUR NAME displays.​

Create a Spring Boot REST Controller

In later assignments we will need to access data from the server using HTTP requests. A common method of accessing data from a server is RES​ T (Representational state transfer) which maps URL patterns and HTTP methods to server side resources. The following sections walk you through exposing different types of data by mapping HTTP request URL patterns to executable methods on the server, usually referred to as R​ EST Web service end points​.

Create a Simple REST Controller

In this section we expose a simple Java String literal as a resource available through an HTTP server request. In STS, under src/main/java​ ,​ create a new class called HelloController​   in package edu.northeastern.cs5200.controllers.hello​   .​ Use the code below as an example, but instead of my name, use YOUR NAME.

 

package edu.northeastern.cs5200.controllers.hello;

 

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

 

@RestController

public class HelloController {

@RequestMapping(“}    “Hello J​            /api/hello/string​o()ose Annunziato!” {​ “)​ ;​ public String return sa​ yHell

}

 

The code above configures the server to listen for an incoming HTTP request with the URL pattern /api/hello/string​ and maps the request to execute the sayHello()​ method. The method returns the string “Hello​ Jose Annunziato!” as an HTTP response, but instead of my name, YOUR NAME displays.

Create a Simple REST JSON Controller

A more interesting use case is to expose structured data such as Java objects of arbitrary complexity. The Java objects can implement a data model that fulfills a particular business need. As a simple example, we’ll create a HelloObject that contains just a String​ message property and expose the trivial data model as a REST HTTP request.

Create a simple POJO (Plain Old Java Object) called HelloObject​ in package edu.northeastern.cs5200​ .controllers.hello to test REST access to data on the server. Use the following code as a guide. Note the property is private and is only accessible through public setters and getters. Also note the required default constructor HelloObject().​

 

package edu.northeastern.cs5200.controllers.hello;

 

public class HelloObject { private String message; public String getMessage() { return message;

}

public void setMessage(String message) { this.message = message;

}

public HelloObject(String message) { this.message = message;

}

public HelloObject() {

}

}

 

To make the above data accessible through an HTTP request (to expose), add an additional @RequestMapping​ that exposes the HelloObject​ to an HTTP request mapped to /api/hello/object​ ​. Add the bolded code below, but instead of my name, use YOUR NAME.

 

package edu.northeastern.cs5200.controllers.hello;

 

import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

 

@RestController

public class HelloController {

@RequestMapping(“/api/hello/string”) public String sayHello() {

}        return “Hello Jose Annunziato​      !​ “;

public HelloObject s​ ay

@RequestMapping(“HelloObject ob/api/hello/object​ HelloObject()j = new HelloObject(“Hello Jose Annunziato!”);​                                      {​ ​“)  

}           return ob​ j;​

}

 

The code above configures the server to listen for an incoming HTTP request with the URL pattern /api/hello/object​ and maps the request to execute the sayHelloObject()​ method. The method returns an instance of HelloObject​ with the message​ property set to “Hello​ Jose Annunziato!”.​ Since the method returns an object instance, instead of a litteral, the object is formatted in an HTTP friendly format such as XM​ L (Extensible Markup Language) or JSO​ N (JavaScript Object Notation).​ Spring boot request controllers format returned objects as JSON by default. Recompile, repackage, and restart the application. Point your browser to

 

http://localhost:8080/api/hello/string

 

and verify the server responds with the string Hello​ Jose Annunziato!,​ but instead of my name, YOUR NAME displays. Then point your browser to

 

http://localhost:8080/api/hello/object

 

and verify the server responds with the following JSON data, but instead of my name, YOUR NAME displays

 

{

“message”:”Hello Jose Annunziato!”

}

 

Note that the JSON attribute “message​ “​ is the same as the ​HelloObject ‘s​ property “message​ “,​ and the value “Hello​ Jose Annunziato!”​ is the value we used to initialize the HelloObject​ instance. In later assignments we will be exploring much more interesting and complex data models.

Deploy a Spring Boot Web Application to AWS

Now that we have tested the Web application running locally, we’ll deploy it to a remote Tomcat server running on Amazon Web Services (AWS). Navigate and login to your AWS console​

 

https://aws.amazon.com/console/

 

Create an Elasti​ c Beanstalk service instance to deploy your Web application. Elastic Beastalk can host any number of applications built on Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker. We’ll be deploying the Java Web application we built on Spring Boot in the previous sections. Follow the steps below to deploy your Java Web application on a remote Tomcat server hosted on an AWS Elastic Beanstalk service.

 

  1. Once you’ve logged in to your AWS console, expand A​ ll services and select E​ lastic Beanstalk​. Click C​ reate New Application on the top right and name the application the same way you named it for the project, e.g., cs5200-spring2018-annunziato, and click create.​
  2. On the left, click on E​ nvironments and then C​ reate one now on the right to create a runtime environment to host the Tomcat server. In the C​ hoose an environment tier dialog choose W​ eb server environment and then Select​.
  3. In the C​ reate a new environment screen choose a public domain for your Web application. Try the same name as the name of the application. Click on C​ heck availability to see if the domain is not already taken. In the unlikely event that the domain is already taken, add a counter to the domain name to distinguish it, e.g, cs5200 -spring2018-annunziato-1​ . Scroll down and choose ​ T​ omcat​ for the P​ latform​.
  4. Down further select U​ pload your code and click on U​ pload​. Browse to the location of the WAR file in the target folder in the location of your project. You made a note of the location of your project in an earlier step. It can be retrieved by right clicking the project, selecting P​ roperties​, then R​ esources​, then L​ ocation on the right. Upload the WAR file, click create environment, and wait for the application to deploy. After a few minutes your application should have deployed.

Test Your Deployed Application

 

Navigate to your Website, e.g.,

 

http://cs5200-spring2018-annunziato.us-west-2.elasticbeanstalk.com/

 

and verify that Hello Jose Annunziato! displays from the root index.html​ file as shown below, but instead of my name,

YOUR NAME displays.

Hello Jose Annunziato!

Then navigate to the REST controllers that returned a string and an object. Add /api/hello​ /string to the URL used in the previous step, e.g., navigate to

 

http://cs5200-spring2018-annunziato.us-west-2.elasticbeanstalk.com/api/hello/string

 

and verify the server responds with the string Hello​ Jose Annunziato!,​ but instead of my name, YOUR NAME displays. Finally, replace string with object in the previous URL, e.g, point your browser to

 

http://cs5200-spring2018-annunziato.us-west-2.elasticbeanstalk.com/api/hello/object

 

and verify the server responds with the following JSON data, but instead of my name, YOUR NAME displays.

 

{

“message”:”Hello Jose Annunziato!”

}

 

Submit all AWS URLs above as deliverables for this assignment, e.g.,

 

http://cs5200-spring2018-annunziato.us-west-2.elasticbeanstalk.com/

http://cs5200-spring2018-annunziato.us-west-2.elasticbeanstalk.com/api/hello/string http://cs5200-spring2018-annunziato.us-west-2.elasticbeanstalk.com/api/hello/object

Commit and Push Your Code to GitHub.com

Create an account at github.com and push your code to a repository named the same as your project, e.g., cs5200-spring2018-annunziato.

 

  1. Navigate and login at github.com
  2. Click on Repositories and click on New to create a new repository
  3. Type the name of the repository in the R​ epository name​ field, e.g., cs5200-spring2018-annunziato, and click on C​ reate repository​. Several commands are shown to initialize, add, commit, and push your code.
  4. On your machine, use the terminal to navigate to your project folder and type the commands github suggests, e.g.,

 

git init git add .

git commit -m “first commit”

git remote add origin https://github.com/jannunzi/cs5200-spring2018-annunziato.git git push -u origin master

 

  1. Where my jannunzi above is my github username and cs5200-spring2018-annunziato is my repository. Use YOUR usename and your repository instead
  2. Submit the URL to your repository as a deliverable for this assignment, e.g.,

 

  • Project-1-1-oae8hx.zip