SlideShare une entreprise Scribd logo
1  sur  40
RESTful Web Services
With Spring MVC
@DigitalSonic
What? Why? How?
Richardson Maturity Model
• How to identify Resources
• How to use HTTP Verbs
• How to design Representations
• Miscellaneous
• Cache/Version/HATEOAS
Agenda
Resources
• Domain Nouns
• Collections & Composites
• Functions
• Controllers
• Article & Comment
• http://domain/articles
• http://domain/articles/1
• http://domain/articles/1/comments
• Web Page
• a composite of article and comments
• Magazine
• a collection of articles
Samples
Samples
• Functions
• http://domain/direction?from=shanghai&to=hangzhou
• Controllers
• http://domain/users/smith/address_merge
Resource Granularity
• Network Efficiency
• Size of Representations
• Client Convenience
URL Mapping in Spring MVC
• <mvc:annotation-driven/>
• @RequestMapping
• @PathVariable
• @RequestParam
• @MatrixVariable
Building URI
• UriComponents
• UriComponentsBuilder
• ServletUriComponentsBuilder
Better URI
• use domains and subdomains to logically group or partition resources
• use / to indicate a hierarchical relationship
• use , and ; to indicate nonhierarchical elements
• use - and _ to improve the readability
• use & to separate parameters
• avoid including file extensions
HTTP Verbs
Verbs
Safety &
Idempotency
Usage
GET Y / Y Use GET for safe and idempotent information retrieval.
POST N / N
Use this method to let the resource perform a variety of actions on the
server side such as creating new resources, updating existing
resources, or making a mixture of changes to one or more resources.
DELETE N / Y Use this method to let a client delete a resource.
PUT N / Y
Use this method to completely update or replace an existing resource
or to create a new resource with a URI specified by the client.
HEAD Y / Y
Use this method to retrieve the same headers as that of a GET
response but without any body in the response.
OPTIONS Y / Y
Use this method to find the list of HTTP methods supported by any
resource or to ping the server.
TRACE Y / Y
Use this method to let the server echo back the headers that it
received.
HTTP Method in Spring MVC
• @RequestMapping
• RequestMethod Enum
HiddenHttpMethodFilter
<input type="hidden" name="_method" value="put"/>
Your web form doesn’t
support so much verbs?
Representations
• Different view technologies in Spring
• JSP/JSTL (InternalResourceViewResolver)
• Tiles (TilesViewResolver)
• XML Marshalling (MarhsallingView)
• JSON Mapping (MappingJacksonJsonView)
• etc.
• @ResponseBody
HTML
Should I say anything
about this?
You know what I mean.
JSON
• Jackson
• MappingJacksonHttpMessageConverter
• Jackson 2
• MappingJackson2HttpMessageConverter
JSON
• @RequestBody
• @ResponseBody
• @RequestMapping
• consumes="application/json"
• produces="application/json"
JSON
• Demo
• Request
• curl -H "Accept: application/json" http://localhost:8080/rest-
demo/articles/10
XML
• JAXB 2
• Jaxb2RootElementHttpMessageConverter
Links
• Link
• href/rel
• Atom link Ref
• self/alternate/related/edit
• first/last/previous/next
Content Negotiation
• serval @RequestMapping with different produces
• contentNegotiationManager
• file extension
• format parameter
• Accept header
• ContentNegotiatingViewResolver
Content Negotiation
HTTP Status Codes
• Use the RIGHT HTTP Status Code!
• @ResponseStatus
• http://www.ietf.org/rfc/rfc2616.txt
HTTP Status Codes
Code Description Code Description
200 OK 400 Bad Request
201 Created 401 Unauthorized
202 Accepted 403 Forbidden
301 Moved Permanently 404 Not Found
303 See Other 410 Gone
304 Not Modified 500 Internal Server Error
307 Temporary Redirect 503 Service Unavailable
Miscellaneous
Cache
• Use the RIGHT Cache HTTP Headers
• Cache-Control
• Expires
• Date
• Last-Modified
Cache for Static Resources
Cache for Dynamic Resources
Support for Etags
• ShallowEtagHeaderFilter
Support for Etags
Version
• Try to maintain the compatibility
• URI
• Representation
• Link
• If you can’t, then make a new version
• subdomain
• path segments
• query parameters
HATEOAS
• Hypermedia as the Engine of Application State
• Spring Hateoas
• https://github.com/SpringSource/spring-hateoas
• Better support for Links and Resources
Demo
References
• RESTful Web Services Cookbook, O’Reilly
• REST in Practice, O’Reilly
• the official reference of the Spring Framework
• and so many articles on the Internet
Let’s Code!!!

Contenu connexe

Tendances

RESTful services
RESTful servicesRESTful services
RESTful services
gouthamrv
 
Representational State Transfer (REST)
Representational State Transfer (REST)Representational State Transfer (REST)
Representational State Transfer (REST)
David Krmpotic
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API Recommendations
Jeelani Shaik
 
Best Practices in Web Service Design
Best Practices in Web Service DesignBest Practices in Web Service Design
Best Practices in Web Service Design
Lorna Mitchell
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
Harry Potter
 

Tendances (19)

Introduction to the Web API
Introduction to the Web APIIntroduction to the Web API
Introduction to the Web API
 
REST & RESTful Web Services
REST & RESTful Web ServicesREST & RESTful Web Services
REST & RESTful Web Services
 
Introduction to RESTful Webservices in JAVA
Introduction to RESTful Webservices  in JAVA Introduction to RESTful Webservices  in JAVA
Introduction to RESTful Webservices in JAVA
 
HTML5 - An introduction
HTML5 - An introductionHTML5 - An introduction
HTML5 - An introduction
 
RESTful Architecture
RESTful ArchitectureRESTful Architecture
RESTful Architecture
 
Understanding REST
Understanding RESTUnderstanding REST
Understanding REST
 
Doing REST Right
Doing REST RightDoing REST Right
Doing REST Right
 
RESTful services
RESTful servicesRESTful services
RESTful services
 
SOAP-based Web Services
SOAP-based Web ServicesSOAP-based Web Services
SOAP-based Web Services
 
Representational State Transfer (REST)
Representational State Transfer (REST)Representational State Transfer (REST)
Representational State Transfer (REST)
 
Angularjs & REST
Angularjs & RESTAngularjs & REST
Angularjs & REST
 
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLEREST Easy with AngularJS - ng-grid CRUD EXAMPLE
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
 
REST API Recommendations
REST API RecommendationsREST API Recommendations
REST API Recommendations
 
The never-ending REST API design debate
The never-ending REST API design debateThe never-ending REST API design debate
The never-ending REST API design debate
 
Best Practices in Web Service Design
Best Practices in Web Service DesignBest Practices in Web Service Design
Best Practices in Web Service Design
 
Learn REST in 18 Slides
Learn REST in 18 SlidesLearn REST in 18 Slides
Learn REST in 18 Slides
 
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRISThe glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
 
How to build a rest api.pptx
How to build a rest api.pptxHow to build a rest api.pptx
How to build a rest api.pptx
 

En vedette

Getting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent EventsGetting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent Events
Arun Gupta
 
Modular Java - OSGi
Modular Java - OSGiModular Java - OSGi
Modular Java - OSGi
Craig Walls
 

En vedette (20)

REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
Microservices with Spring Boot
Microservices with Spring BootMicroservices with Spring Boot
Microservices with Spring Boot
 
Spring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. RESTSpring Web Services: SOAP vs. REST
Spring Web Services: SOAP vs. REST
 
初窥Java网络IO
初窥Java网络IO初窥Java网络IO
初窥Java网络IO
 
实战HotSpot JVM GC
实战HotSpot JVM GC实战HotSpot JVM GC
实战HotSpot JVM GC
 
Hadoop introduction
Hadoop introductionHadoop introduction
Hadoop introduction
 
Overall enterprise application architecture
Overall enterprise application architectureOverall enterprise application architecture
Overall enterprise application architecture
 
A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom A Walking Tour of (almost) all of Springdom
A Walking Tour of (almost) all of Springdom
 
Thinking Beyond ORM in JPA
Thinking Beyond ORM in JPAThinking Beyond ORM in JPA
Thinking Beyond ORM in JPA
 
Lazy vs. Eager Loading Strategies in JPA 2.1
Lazy vs. Eager Loading Strategies in JPA 2.1Lazy vs. Eager Loading Strategies in JPA 2.1
Lazy vs. Eager Loading Strategies in JPA 2.1
 
Hibernate using jpa
Hibernate using jpaHibernate using jpa
Hibernate using jpa
 
Colloquium Report
Colloquium ReportColloquium Report
Colloquium Report
 
Getting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent EventsGetting Started with WebSockets and Server-Sent Events
Getting Started with WebSockets and Server-Sent Events
 
Restful webservice
Restful webserviceRestful webservice
Restful webservice
 
Java 8 - New Features
Java 8 - New FeaturesJava 8 - New Features
Java 8 - New Features
 
Secure Authentication and Session Management in Java EE
Secure Authentication and Session Management in Java EESecure Authentication and Session Management in Java EE
Secure Authentication and Session Management in Java EE
 
Modular Java - OSGi
Modular Java - OSGiModular Java - OSGi
Modular Java - OSGi
 
That old Spring magic has me in its SpEL
That old Spring magic has me in its SpELThat old Spring magic has me in its SpEL
That old Spring magic has me in its SpEL
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring Boot. Boot up your development
Spring Boot. Boot up your developmentSpring Boot. Boot up your development
Spring Boot. Boot up your development
 

Similaire à RESTful Web Services with Spring MVC

Rest presentation
Rest  presentationRest  presentation
Rest presentation
srividhyau
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 

Similaire à RESTful Web Services with Spring MVC (20)

Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
 
StackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStackStackMate - CloudFormation for CloudStack
StackMate - CloudFormation for CloudStack
 
PLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring SurfPLAT-8 Spring Web Scripts and Spring Surf
PLAT-8 Spring Web Scripts and Spring Surf
 
Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012Backbonification for dummies - Arrrrug 10/1/2012
Backbonification for dummies - Arrrrug 10/1/2012
 
Rest presentation
Rest  presentationRest  presentation
Rest presentation
 
Rest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.jsRest with Java EE 6 , Security , Backbone.js
Rest with Java EE 6 , Security , Backbone.js
 
Rest
RestRest
Rest
 
Java EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVCJava EE 8 Web Frameworks: A Look at JSF vs MVC
Java EE 8 Web Frameworks: A Look at JSF vs MVC
 
Rest And Rails
Rest And RailsRest And Rails
Rest And Rails
 
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
 
The ASP.NET Web API for Beginners
The ASP.NET Web API for BeginnersThe ASP.NET Web API for Beginners
The ASP.NET Web API for Beginners
 
Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5
 
RESTful HATEOAS standards using Java based Katharsis
RESTful HATEOAS standards using Java based KatharsisRESTful HATEOAS standards using Java based Katharsis
RESTful HATEOAS standards using Java based Katharsis
 
RESTful HATEOAS standards using Java based Katharsis
RESTful HATEOAS standards using Java based KatharsisRESTful HATEOAS standards using Java based Katharsis
RESTful HATEOAS standards using Java based Katharsis
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
MVC 1.0 als alternative Webtechnologie
MVC 1.0 als alternative WebtechnologieMVC 1.0 als alternative Webtechnologie
MVC 1.0 als alternative Webtechnologie
 
Single Page Applications: Your Browser is the OS!
Single Page Applications: Your Browser is the OS!Single Page Applications: Your Browser is the OS!
Single Page Applications: Your Browser is the OS!
 
The Spring Update
The Spring UpdateThe Spring Update
The Spring Update
 
Ajug - The Spring Update
Ajug - The Spring UpdateAjug - The Spring Update
Ajug - The Spring Update
 
Angular js 1.0-fundamentals
Angular js 1.0-fundamentalsAngular js 1.0-fundamentals
Angular js 1.0-fundamentals
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

RESTful Web Services with Spring MVC