SlideShare a Scribd company logo
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:
Ad

More Related Content

What's hot (20)

Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2
Yakov Fain
 
Angular2 Development for Java developers
Angular2 Development for Java developersAngular2 Development for Java developers
Angular2 Development for Java developers
Yakov Fain
 
[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
Yakov Fain
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
Emprovise
 
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...
Katy Slemon
 
Intro react js
Intro react jsIntro react js
Intro react js
Vijayakanth MP
 
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
Mindfire Solutions
 
React outbox
React outboxReact outbox
React outbox
Angela Lehru
 
Introduction to Spring MVC
Introduction to Spring MVCIntroduction to Spring MVC
Introduction to Spring MVC
Richard Paul
 
Spring 3.x - Spring MVC
Spring 3.x - Spring MVCSpring 3.x - Spring MVC
Spring 3.x - Spring MVC
Guy Nir
 
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
Katy Slemon
 
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
Yakov Fain
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
Ran Wahle
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
Yakov Fain
 
Java web application development
Java web application developmentJava web application development
Java web application development
RitikRathaur
 
Spring MVC Annotations
Spring MVC AnnotationsSpring MVC Annotations
Spring MVC Annotations
Jordan Silva
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)
Hsuan Fu Lien
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
Joshua Long
 
Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2Reactive Thinking in Java with RxJava2
Reactive Thinking in Java with RxJava2
Yakov Fain
 
Angular2 Development for Java developers
Angular2 Development for Java developersAngular2 Development for Java developers
Angular2 Development for Java developers
Yakov Fain
 
[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
Yakov Fain
 
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...
Katy Slemon
 
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
Mindfire Solutions
 
Introduction to Spring MVC
Introduction to Spring MVCIntroduction to Spring MVC
Introduction to Spring MVC
Richard Paul
 
Spring 3.x - Spring MVC
Spring 3.x - Spring MVCSpring 3.x - Spring MVC
Spring 3.x - Spring MVC
Guy Nir
 
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
Katy Slemon
 
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
Yakov Fain
 
Angular js 2
Angular js 2Angular js 2
Angular js 2
Ran Wahle
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
Yakov Fain
 
Java web application development
Java web application developmentJava web application development
Java web application development
RitikRathaur
 
Spring MVC Annotations
Spring MVC AnnotationsSpring MVC Annotations
Spring MVC Annotations
Jordan Silva
 
Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)Introduce Flux & react in practices (KKBOX)
Introduce Flux & react in practices (KKBOX)
Hsuan Fu Lien
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
Joshua Long
 

Similar to Rest web service_with_spring_hateoas (20)

Implementing a file manager in ASP.NET Core 8.0
Implementing a file manager in ASP.NET Core 8.0Implementing a file manager in ASP.NET Core 8.0
Implementing a file manager in ASP.NET Core 8.0
GleamTech Dev
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
mirrec
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample Chapter
Syed Shahul
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
Minal Maniar
 
Full Stack_Reac web Development and Application
Full Stack_Reac web Development and ApplicationFull Stack_Reac web Development and Application
Full Stack_Reac web Development and Application
Jeyarajs7
 
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...
OdessaJS Conf
 
Spring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 IntegrationSpring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 Integration
Majurageerthan Arumugathasan
 
Spring MVC 5 & Hibernate 5 Integration.pdf
Spring MVC 5 & Hibernate 5 Integration.pdfSpring MVC 5 & Hibernate 5 Integration.pdf
Spring MVC 5 & Hibernate 5 Integration.pdf
Patiento Del Mar
 
Love at first Vue
Love at first VueLove at first Vue
Love at first Vue
Dalibor Gogic
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
Sapna Upreti
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
Steven Smith
 
React loadable
React loadableReact loadable
React loadable
George Bukhanov
 
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
Antônio Roberto Silva
 
Deploying configurable frontend web application containers
Deploying configurable frontend web application containersDeploying configurable frontend web application containers
Deploying configurable frontend web application containers
José Moreira
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892
Tuna Tore
 
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
 
Spring Boot
Spring BootSpring Boot
Spring Boot
HongSeong Jeon
 
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
Lou Sacco
 
asp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).ppt
asp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).pptasp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).ppt
asp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).ppt
Anwar Patel
 
Jdbc
JdbcJdbc
Jdbc
BindhuBhargaviTalasi
 
Implementing a file manager in ASP.NET Core 8.0
Implementing a file manager in ASP.NET Core 8.0Implementing a file manager in ASP.NET Core 8.0
Implementing a file manager in ASP.NET Core 8.0
GleamTech Dev
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
mirrec
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample Chapter
Syed Shahul
 
Java servlet technology
Java servlet technologyJava servlet technology
Java servlet technology
Minal Maniar
 
Full Stack_Reac web Development and Application
Full Stack_Reac web Development and ApplicationFull Stack_Reac web Development and Application
Full Stack_Reac web Development and Application
Jeyarajs7
 
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...
OdessaJS Conf
 
Spring MVC 5 & Hibernate 5 Integration.pdf
Spring MVC 5 & Hibernate 5 Integration.pdfSpring MVC 5 & Hibernate 5 Integration.pdf
Spring MVC 5 & Hibernate 5 Integration.pdf
Patiento Del Mar
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
Sapna Upreti
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
Steven Smith
 
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
Antônio Roberto Silva
 
Deploying configurable frontend web application containers
Deploying configurable frontend web application containersDeploying configurable frontend web application containers
Deploying configurable frontend web application containers
José Moreira
 
springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892springmvc-150923124312-lva1-app6892
springmvc-150923124312-lva1-app6892
Tuna Tore
 
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
 
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
Lou Sacco
 
asp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).ppt
asp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).pptasp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).ppt
asp-2005311dgvdfvdfvfdfvdvfdbfdb03252 (1).ppt
Anwar Patel
 
Ad

Recently uploaded (19)

5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx
andani26
 
White and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptxWhite and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptx
canumatown
 
Perguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolhaPerguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolha
socaslev
 
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 SupportReliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
steve198109
 
highend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptxhighend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptx
elhadjcheikhdiop
 
APNIC Update, presented at NZNOG 2025 by Terry Sweetser
APNIC Update, presented at NZNOG 2025 by Terry SweetserAPNIC Update, presented at NZNOG 2025 by Terry Sweetser
APNIC Update, presented at NZNOG 2025 by Terry Sweetser
APNIC
 
Determining Glass is mechanical textile
Determining  Glass is mechanical textileDetermining  Glass is mechanical textile
Determining Glass is mechanical textile
Azizul Hakim
 
IT Services Workflow From Request to Resolution
IT Services Workflow From Request to ResolutionIT Services Workflow From Request to Resolution
IT Services Workflow From Request to Resolution
mzmziiskd
 
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
DataProvider1
 
project_based_laaaaaaaaaaearning,kelompok 10.pptx
project_based_laaaaaaaaaaearning,kelompok 10.pptxproject_based_laaaaaaaaaaearning,kelompok 10.pptx
project_based_laaaaaaaaaaearning,kelompok 10.pptx
redzuriel13
 
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC
 
Best web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you businessBest web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you business
steve198109
 
Understanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep WebUnderstanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep Web
nabilajabin35
 
(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security
aluacharya169
 
OSI TCP IP Protocol Layers description f
OSI TCP IP Protocol Layers description fOSI TCP IP Protocol Layers description f
OSI TCP IP Protocol Layers description f
cbr49917
 
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHostingTop Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
steve198109
 
DNS Resolvers and Nameservers (in New Zealand)
DNS Resolvers and Nameservers (in New Zealand)DNS Resolvers and Nameservers (in New Zealand)
DNS Resolvers and Nameservers (in New Zealand)
APNIC
 
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation TemplateSmart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
yojeari421237
 
Computers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers NetworksComputers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers Networks
Tito208863
 
5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx5-Proses-proses Akuisisi Citra Digital.pptx
5-Proses-proses Akuisisi Citra Digital.pptx
andani26
 
White and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptxWhite and Red Clean Car Business Pitch Presentation.pptx
White and Red Clean Car Business Pitch Presentation.pptx
canumatown
 
Perguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolhaPerguntas dos animais - Slides ilustrados de múltipla escolha
Perguntas dos animais - Slides ilustrados de múltipla escolha
socaslev
 
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 SupportReliable Vancouver Web Hosting with Local Servers & 24/7 Support
Reliable Vancouver Web Hosting with Local Servers & 24/7 Support
steve198109
 
highend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptxhighend-srxseries-services-gateways-customer-presentation.pptx
highend-srxseries-services-gateways-customer-presentation.pptx
elhadjcheikhdiop
 
APNIC Update, presented at NZNOG 2025 by Terry Sweetser
APNIC Update, presented at NZNOG 2025 by Terry SweetserAPNIC Update, presented at NZNOG 2025 by Terry Sweetser
APNIC Update, presented at NZNOG 2025 by Terry Sweetser
APNIC
 
Determining Glass is mechanical textile
Determining  Glass is mechanical textileDetermining  Glass is mechanical textile
Determining Glass is mechanical textile
Azizul Hakim
 
IT Services Workflow From Request to Resolution
IT Services Workflow From Request to ResolutionIT Services Workflow From Request to Resolution
IT Services Workflow From Request to Resolution
mzmziiskd
 
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...Mobile database for your company telemarketing or sms marketing campaigns. Fr...
Mobile database for your company telemarketing or sms marketing campaigns. Fr...
DataProvider1
 
project_based_laaaaaaaaaaearning,kelompok 10.pptx
project_based_laaaaaaaaaaearning,kelompok 10.pptxproject_based_laaaaaaaaaaearning,kelompok 10.pptx
project_based_laaaaaaaaaaearning,kelompok 10.pptx
redzuriel13
 
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC -Policy Development Process, presented at Local APIGA Taiwan 2025
APNIC
 
Best web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you businessBest web hosting Vancouver 2025 for you business
Best web hosting Vancouver 2025 for you business
steve198109
 
Understanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep WebUnderstanding the Tor Network and Exploring the Deep Web
Understanding the Tor Network and Exploring the Deep Web
nabilajabin35
 
(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security(Hosting PHising Sites) for Cryptography and network security
(Hosting PHising Sites) for Cryptography and network security
aluacharya169
 
OSI TCP IP Protocol Layers description f
OSI TCP IP Protocol Layers description fOSI TCP IP Protocol Layers description f
OSI TCP IP Protocol Layers description f
cbr49917
 
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHostingTop Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
Top Vancouver Green Business Ideas for 2025 Powered by 4GoodHosting
steve198109
 
DNS Resolvers and Nameservers (in New Zealand)
DNS Resolvers and Nameservers (in New Zealand)DNS Resolvers and Nameservers (in New Zealand)
DNS Resolvers and Nameservers (in New Zealand)
APNIC
 
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation TemplateSmart Mobile App Pitch Deck丨AI Travel App Presentation Template
Smart Mobile App Pitch Deck丨AI Travel App Presentation Template
yojeari421237
 
Computers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers NetworksComputers Networks Computers Networks Computers Networks
Computers Networks Computers Networks Computers Networks
Tito208863
 
Ad

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: