1. Rest Web Service with Spring
The goal of this lab work is to develop a small application for cars renting.
The functionalities to implements are:
- Get a list of unrented cars
- Rent a car
- Get back a car
Those functionalities are defined by the following interface:
package web;
import java.util.List;
import org.springframework.hateoas.ResourceSupport;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import dto.CarDTO;
public interface RentService {
@RequestMapping(value = "/entryPoint", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ResourceSupport get();
/**
*
* @return all cars not rented
*/
@RequestMapping(value = "/car", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public List<CarDTO> getCars();
/**
* Return specifications of a car.
* @param plateNumber
* @return car specifications only (if not rented)
* @throws Exception no car with this plate number or already rented
*/
@RequestMapping(value = "/car/{plateNumber}", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public CarDTO getCar(@PathVariable("plateNumber") String plateNumber) throws Exception;
/**
* Rent a car.
* @param plateNumber
* @return car specifications
* @throws Exception no car with this plate number or already rented
*/
@RequestMapping(value = "/car/{plateNumber}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void rentCar(@PathVariable("plateNumber") String plateNumber) throws Exception;
/**
* Get back a rented car.
* @param plateNumber
2. * @return actions to be done
* @throws Exception no car with this plate number or not rented
*/
@RequestMapping(value = "/car/{plateNumber}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public void renderCar(@PathVariable("plateNumber") String plateNumber) throws Exception;
}
Get the application core on Github
The core of the application is on github at:
https://github.com/charroux/GradleSpringRestBasis
Download the entire project as zip file and uncompress it on your machine.
The project is a Gradle project (http://www.gradle.org/).
Gradle installation (if necessary)
Download gradle: http://www.gradle.org/downloads (binary version is enough).
Uncompress the file.
Set two environment variables:
If used under windows, without administrator rights, you can set a variable in a command window
with:
set GRADLE_HOME …
Don’t close the window to keep the variables set.
Add GRADLE_HOMEbin to the variable path to use gradle from anywhere on your machine.
Using Gradle
To convert the project into an Eclipse project: gradle eclipse
To build all the project: gradle build
To launch the embedded web server jetty : gradle jettyRunWar
Using Eclipse to edit the source files
Download and uncompress the Apache/Tomcat web server (version binary 7.x)
http://tomcat.apache.org/
3. Add a Server Runtime to Eclipse: Windows –> Preferences -> Server -> Runtime environments -> Add
Import the project into Eclipse: File -> import –> General -> Existing project into workspace
Server web launching: write clic on the project -> Run as -> Run on server
Study of the application
Imported under Eclipse (don’t forget to use the Gradle commands first), the project looks like this:
The file build.gradle is a configuration file for Gradle. It indicates which libraries are necessary:
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.hateoas:spring-hateoas:0.9.0.RELEASE'
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.12'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
providedCompile 'org.apache.tomcat:tomcat-catalina:7.0.47'
4. testCompile group: 'junit', name: 'junit', version: '4.5'
}
It also declares witch plug-in can be used: eclipse, jetty…
The package model contains the data structure: here a unique class defining a car for renting.
The package services contains a cars container named CarManager: it allows to get a list of cars.
The package dto (for Data Transfert Object) contains a class declaring data to be sent through the
network: a java object will be converted into JSon object.
Finally, the package web contains the java interface RentService defining the functionalities and the
class to be coded.
The web part of the application is in the src folder:
It contains two configuration files:
- web.xml (Java standard for web application)
- do-servlet.xml defining the configuration of the Spring framework
Mainly do-servlet.xml declares the Spring controller implementing the RentService interface, and a
converter from Java to JSon (or vice versa):
<bean id="siteController" class="web.MyRentController"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
5. <property name="supportedMediaTypes" value="application/json" />
</bean>
Questions to answer before starting coding
What is a HATEOAS?
Explain the pattern TDO? What is this pattern for?
How the Web Service can serve JSon form Java?
Look at the file web.xml. What are the following instructions for?
<servlet-
mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
How the Web Service can serve JSon form Java ?
Rest controller coding
Implement the RentService interface into the Spring Controller MyRentController.
Test your code by requesting the following URI:
Or choose a car by its plate number:
Java client project
The core of the application is on github at:
https://github.com/charroux/GradleSpringRestClientBasis
Download the entire project as zip file and uncompress it on your machine.
The project is a Gradle project.
To convert the project into an Eclipse project: gradle eclipse
To build all the project: gradle build
The project should appear has follow:
6. Check if the URI of the web service is the right one in the file Client.java (it depends on the project
name at the server side).
Launch this file. Explain what’s happen.
Questions to answer before to continue
What is the class org.springframework.web.client.RestTemplate for?
Is it possible to choose which Java attributes are sent in JSon?
Client coding coding
Modify the Java client in order to send HTTP put, post and delete requests.
Web client coding (advanced users)
Using Java Script and JQuery, create a web interface for car renting: