SlideShare une entreprise Scribd logo
1  sur  6
Télécharger pour lire hors ligne
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
* @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/
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'
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">
<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:
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:

Contenu connexe

Tendances

[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
洪 鹏发
 
Spring 3.x - Spring MVC
Spring 3.x - Spring MVCSpring 3.x - Spring MVC
Spring 3.x - Spring MVC
Guy Nir
 

Tendances (20)

Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2
 
Angular2 Development for Java developers
Angular2 Development for Java developersAngular2 Development for Java developers
Angular2 Development for Java developers
 
[Final] ReactJS presentation
[Final] ReactJS presentation[Final] ReactJS presentation
[Final] ReactJS presentation
 
Web sockets in Angular
Web sockets in AngularWeb sockets in Angular
Web sockets in Angular
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
SpringMVC
SpringMVCSpringMVC
SpringMVC
 
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
How to Implement Basic Angular Routing and Nested Routing With Params in Angu...
 
Intro react js
Intro react jsIntro react js
Intro react js
 
ASP.NET Page life cycle and ViewState
ASP.NET Page life cycle and ViewStateASP.NET Page life cycle and ViewState
ASP.NET Page life cycle and ViewState
 
React outbox
React outboxReact outbox
React outbox
 
Introduction to Spring MVC
Introduction to Spring MVCIntroduction to Spring MVC
Introduction to Spring MVC
 
Spring 3.x - Spring MVC
Spring 3.x - Spring MVCSpring 3.x - Spring MVC
Spring 3.x - Spring MVC
 
React native app with type script tutorial
React native app with type script tutorialReact native app with type script tutorial
React native app with type script tutorial
 
RESTful services and OAUTH protocol in IoT
RESTful services and OAUTH protocol in IoTRESTful services and OAUTH protocol in IoT
RESTful services and OAUTH protocol in IoT
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
Java web application development
Java web application developmentJava web application development
Java web application development
 
Spring MVC Annotations
Spring MVC AnnotationsSpring MVC Annotations
Spring MVC Annotations
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
 

Similaire à Rest web service_with_spring_hateoas

Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample Chapter
Syed Shahul
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
Tuna Tore
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892
Tuna Tore
 

Similaire à Rest web service_with_spring_hateoas (20)

Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample Chapter
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
 
Spring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 IntegrationSpring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 Integration
 
Love at first Vue
Love at first VueLove at first Vue
Love at first Vue
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
 
React loadable
React loadableReact loadable
React loadable
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Deploying configurable frontend web application containers
Deploying configurable frontend web application containersDeploying configurable frontend web application containers
Deploying configurable frontend web application containers
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014Meteor Meet-up San Diego December 2014
Meteor Meet-up San Diego December 2014
 
Jdbc
JdbcJdbc
Jdbc
 
Workshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSWorkshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJS
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applications
 
ReactJS
ReactJSReactJS
ReactJS
 
How to implement multiple layouts using React router V4.pptx
How to implement multiple layouts using React router V4.pptxHow to implement multiple layouts using React router V4.pptx
How to implement multiple layouts using React router V4.pptx
 

Dernier

₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
Diya Sharma
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
ellan12
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
sexy call girls service in goa
 

Dernier (20)

'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
𓀤Call On 7877925207 𓀤 Ahmedguda Call Girls Hot Model With Sexy Bhabi Ready Fo...
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
 
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark WebGDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
GDG Cloud Southlake 32: Kyle Hettinger: Demystifying the Dark Web
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
 
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
VIP Model Call Girls NIBM ( Pune ) Call ON 8005736733 Starting From 5K to 25K...
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Rani Bagh Escort Service Delhi N.C.R.
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
WhatsApp 📞 8448380779 ✅Call Girls In Mamura Sector 66 ( Noida)
 
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptxAWS Community DAY Albertini-Ellan Cloud Security (1).pptx
AWS Community DAY Albertini-Ellan Cloud Security (1).pptx
 
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 

Rest web service_with_spring_hateoas

  • 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: