Skip to content

Latest commit

 

History

History
279 lines (222 loc) · 7.61 KB

README.md

File metadata and controls

279 lines (222 loc) · 7.61 KB

Java Spring Phone Catalog Tutorial Application Build Status

Overview

This is an example application/tutorial learn Spring, REST and other Java technologies.

This application takes the developer through the process of building a web-application using Spring. The application is based on the Angularjs Phone Catalog Tutorial. The idea is to create the back-end application for this tutorial.

Each tagged commit is a separate lesson teaching an aspect of generating a Restful Java web server with Spring.

Prerequisites

Git

  • A good place to learn about setting up git is here.
  • Git home (download, documentation).

Java and maven

Workings of the application

  • Read the Development section at the end to familiarize yourself with running and developing an angular application.

Commits / Tutorial Outline

You can check out any point of the tutorial using git checkout step-?

To see the changes which between any two lessons use the git diff command. git diff step-?..step-?

step-0

  • Install Java
  • Install maven
  • Install git
  • Instal mongodb
  • Clone repository
  • run: mvn clean package to import dependencies and check environment status

step-1

  • Add project encoding:
<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
  • Add java compilation level in pom.xml
<plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>2.3.2</version>
 <configuration>
  <source>1.7</source>
  <target>1.7</target>
 </configuration>
</plugin>
  • Change junit vesion to 4.12

  • Add WAR plugin for webapp packaging

<packaging>war</packaging>
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-war-plugin</artifactId>
 <version>2.6</version>
 <configuration>
  <failOnMissingWebXml>false</failOnMissingWebXml>
 </configuration>
</plugin>
  • Make maven work with servlet 3 config adding to pom:
<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 <scope>provided</scope>
</dependency>
  • Add Spring MVC dependency:
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>4.0.8.RELEASE</version>
</dependency>
  • Add jetty server to deploy and run the webapp
<plugin>
 <groupId>org.eclipse.jetty</groupId>
 <artifactId>jetty-maven-plugin</artifactId>
 <version>9.3.0.M1</version>
 <configuration>
  <scanIntervalSeconds>3</scanIntervalSeconds>
  <httpConnector>
   <port>9000</port>
  </httpConnector>
 </configuration>
</plugin>

step-2

  • Add User Entity
  • Store in DB
  • Add Spring Data Mongo dependency to POM
<!-- Spring data mongodb -->
<dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-mongodb</artifactId>
 <version>1.2.0.RELEASE</version>
</dependency>
  • Add MongoConfiguration
  • Add MongoConfiguration to root configurations in MyWebAppInitializer
  • Add Anotations and Repository
  • Return as REST web service
  • Add json dependency in pom
<!-- Jackson Core -->
<dependency>
 <groupId>com.fasterxml.jackson.core</groupId>
 <artifactId>jackson-databind</artifactId>
 <version>2.3.4</version>
</dependency>
  • Add User RestController
  • Add POST method to store User
  • Add GET method to retrieve User
  • Install Advanced rest client chrome
  • Add log4j dependencies in pom
<!-- Log4J2 -->
<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-slf4j-impl</artifactId>
 <version>2.1</version>
</dependency>
<dependency>
 <groupId>org.apache.logging.log4j</groupId>
 <artifactId>log4j-core</artifactId>
 <version>2.1</version>
</dependency>
  • Add log4j file

step-3

  • Add Spring security dependencies to pom
<!-- Spring security -->
<dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-config</artifactId>
 <version>3.2.3.RELEASE</version>
</dependency>
<dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-web</artifactId>
 <version>3.2.3.RELEASE</version>
</dependency>

step-4

  • Add phone.json to resources.
  • Add jsonschema2pojo plugin to pom
<plugin>
 <groupId>org.jsonschema2pojo</groupId>
 <artifactId>jsonschema2pojo-maven-plugin</artifactId>
 <version>0.4.15</version>
 <configuration>
  <sourceDirectory>${basedir}/src/main/resources/schema/phone.json</sourceDirectory>
  <targetPackage>com.examplecorp.phonecat.model</targetPackage>
  <includeHashcodeAndEquals>false</includeHashcodeAndEquals>
  <includeToString>false</includeToString>
  <sourceType>json</sourceType>
 </configuration>
 <executions>
  <execution>
   <goals>
    <goal>generate</goal>
   </goals>
  </execution>
 </executions>
</plugin>
  • Create mongo repository and controller to use Phone class

Development with spring-phonecat

The following docs describe how you can test and develop further this application.

Installing dependencies

The application relies upon maven. You can install these by running:

mvn clean install

Running the app during development

  • Run mvn jetty:run
  • Use Chrome extension Advanced rest client with the url http://localhost:9000 to see the app running in your browser.