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.
- JDK 1.7.x Ubuntu/Debian Windows
- maven3
- mongodb
- Read the Development section at the end to familiarize yourself with running and developing an angular application.
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-?
- Install Java
- Install maven
- Install git
- Instal mongodb
- Clone repository
- run:
mvn clean package
to import dependencies and check environment status
- 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>
-
Create classes: MyWebAppInitializer MyWebConfig Add ViewResolver bean to MyWebConfig HelloWorldmentController Add home.jsp file to
src/main/WEB-INF/jsp/
-
Delete default generated App class and test
-
Run the webapp
mvn jetty:run
-
Open the webapp
- 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
- 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>
- Create classes: SecurityApplicationInitializer AppUserDetailsService SecurityConfiguration [CustomAuthenticationSucessHandler]
- Add [SecurityConfiguration] to [MyWebAppInitializer]
- Make Controllers and entities aware of the logged user
- Make the user not return the password
- Create [MethodSecurityConfiguration]
- Make get User method secure
- 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
The following docs describe how you can test and develop further this application.
The application relies upon maven. You can install these by running:
mvn clean install
- Run
mvn jetty:run
- Use Chrome extension Advanced rest client with the url
http://localhost:9000
to see the app running in your browser.