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

More Related Content

What's hot (19)

PPT
Introduction to the Web API
Brad Genereaux
 
PPTX
REST & RESTful Web Services
Halil Burak Cetinkaya
 
PPTX
Introduction to RESTful Webservices in JAVA
psrpatnaik
 
PDF
HTML5 - An introduction
Eleonora Ciceri
 
PPTX
RESTful Architecture
Kabir Baidya
 
PPT
Understanding REST
Nitin Pande
 
PDF
Doing REST Right
Kerry Buckley
 
PPT
RESTful services
gouthamrv
 
PDF
SOAP-based Web Services
Katrien Verbert
 
PDF
Representational State Transfer (REST)
David Krmpotic
 
PDF
Angularjs & REST
Corley S.r.l.
 
PPTX
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
reneechemel
 
PDF
REST API Recommendations
Jeelani Shaik
 
PDF
The never-ending REST API design debate
Restlet
 
PDF
Best Practices in Web Service Design
Lorna Mitchell
 
PDF
Learn REST in 18 Slides
Suraj Gupta
 
PPTX
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
Geert Pante
 
PPTX
Rest and Rails
Chamnap Chhorn
 
PDF
How to build a rest api.pptx
Harry Potter
 
Introduction to the Web API
Brad Genereaux
 
REST & RESTful Web Services
Halil Burak Cetinkaya
 
Introduction to RESTful Webservices in JAVA
psrpatnaik
 
HTML5 - An introduction
Eleonora Ciceri
 
RESTful Architecture
Kabir Baidya
 
Understanding REST
Nitin Pande
 
Doing REST Right
Kerry Buckley
 
RESTful services
gouthamrv
 
SOAP-based Web Services
Katrien Verbert
 
Representational State Transfer (REST)
David Krmpotic
 
Angularjs & REST
Corley S.r.l.
 
REST Easy with AngularJS - ng-grid CRUD EXAMPLE
reneechemel
 
REST API Recommendations
Jeelani Shaik
 
The never-ending REST API design debate
Restlet
 
Best Practices in Web Service Design
Lorna Mitchell
 
Learn REST in 18 Slides
Suraj Gupta
 
The glory of REST in Java: Spring HATEOAS, RAML, Temenos IRIS
Geert Pante
 
Rest and Rails
Chamnap Chhorn
 
How to build a rest api.pptx
Harry Potter
 

Viewers also liked (20)

PDF
REST APIs with Spring
Joshua Long
 
PDF
Microservices with Spring Boot
Joshua Long
 
PDF
Spring Web Services: SOAP vs. REST
Sam Brannen
 
PDF
初窥Java网络IO
digitalsonic
 
PDF
实战HotSpot JVM GC
digitalsonic
 
PPTX
Hadoop introduction
Dong Ngoc
 
PPTX
Overall enterprise application architecture
Dong Ngoc
 
KEY
A Walking Tour of (almost) all of Springdom
Joshua Long
 
PPTX
Thinking Beyond ORM in JPA
Patrycja Wegrzynowicz
 
PDF
Lazy vs. Eager Loading Strategies in JPA 2.1
Patrycja Wegrzynowicz
 
PDF
Hibernate using jpa
Mohammad Faizan
 
DOCX
Colloquium Report
Mohammad Faizan
 
PDF
Getting Started with WebSockets and Server-Sent Events
Arun Gupta
 
PPTX
Restful webservice
Dong Ngoc
 
PDF
Java 8 - New Features
Rafael Brito de Oliveira
 
PDF
Secure Authentication and Session Management in Java EE
Patrycja Wegrzynowicz
 
KEY
Modular Java - OSGi
Craig Walls
 
PDF
That old Spring magic has me in its SpEL
Craig Walls
 
PDF
Spring boot
Bhagwat Kumar
 
PPT
Spring Boot. Boot up your development
Strannik_2013
 
REST APIs with Spring
Joshua Long
 
Microservices with Spring Boot
Joshua Long
 
Spring Web Services: SOAP vs. REST
Sam Brannen
 
初窥Java网络IO
digitalsonic
 
实战HotSpot JVM GC
digitalsonic
 
Hadoop introduction
Dong Ngoc
 
Overall enterprise application architecture
Dong Ngoc
 
A Walking Tour of (almost) all of Springdom
Joshua Long
 
Thinking Beyond ORM in JPA
Patrycja Wegrzynowicz
 
Lazy vs. Eager Loading Strategies in JPA 2.1
Patrycja Wegrzynowicz
 
Hibernate using jpa
Mohammad Faizan
 
Colloquium Report
Mohammad Faizan
 
Getting Started with WebSockets and Server-Sent Events
Arun Gupta
 
Restful webservice
Dong Ngoc
 
Java 8 - New Features
Rafael Brito de Oliveira
 
Secure Authentication and Session Management in Java EE
Patrycja Wegrzynowicz
 
Modular Java - OSGi
Craig Walls
 
That old Spring magic has me in its SpEL
Craig Walls
 
Spring boot
Bhagwat Kumar
 
Spring Boot. Boot up your development
Strannik_2013
 
Ad

Similar to RESTful Web Services with Spring MVC (20)

PDF
Java colombo-deep-dive-into-jax-rs
Sagara Gunathunga
 
PPTX
StackMate - CloudFormation for CloudStack
Chiradeep Vittal
 
PDF
PLAT-8 Spring Web Scripts and Spring Surf
Alfresco Software
 
KEY
Backbonification for dummies - Arrrrug 10/1/2012
Dimitri de Putte
 
PPTX
Rest presentation
srividhyau
 
PPTX
Rest
Carol McDonald
 
PPTX
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
PDF
Java EE 8 Web Frameworks: A Look at JSF vs MVC
Josh Juneau
 
PDF
Rest And Rails
Kaushik Jha
 
PDF
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
SPTechCon
 
PPTX
18CSC311J Web Design and Development UNIT-3
Sivakumar M
 
PPTX
The ASP.NET Web API for Beginners
Kevin Hazzard
 
PPTX
Session 29 - Servlets - Part 5
PawanMM
 
PDF
RESTful HATEOAS standards using Java based Katharsis
Keith Moore
 
PDF
RESTful HATEOAS standards using Java based Katharsis
Keith Moore
 
PPTX
Spring mvc
Harshit Choudhary
 
PDF
MVC 1.0 als alternative Webtechnologie
OPEN KNOWLEDGE GmbH
 
PPTX
Single Page Applications: Your Browser is the OS!
Jeremy Likness
 
PDF
The Spring Update
Gunnar Hillert
 
PDF
Ajug - The Spring Update
Gunnar Hillert
 
Java colombo-deep-dive-into-jax-rs
Sagara Gunathunga
 
StackMate - CloudFormation for CloudStack
Chiradeep Vittal
 
PLAT-8 Spring Web Scripts and Spring Surf
Alfresco Software
 
Backbonification for dummies - Arrrrug 10/1/2012
Dimitri de Putte
 
Rest presentation
srividhyau
 
Rest with Java EE 6 , Security , Backbone.js
Carol McDonald
 
Java EE 8 Web Frameworks: A Look at JSF vs MVC
Josh Juneau
 
Rest And Rails
Kaushik Jha
 
Tutorial, Part 2: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
SPTechCon
 
18CSC311J Web Design and Development UNIT-3
Sivakumar M
 
The ASP.NET Web API for Beginners
Kevin Hazzard
 
Session 29 - Servlets - Part 5
PawanMM
 
RESTful HATEOAS standards using Java based Katharsis
Keith Moore
 
RESTful HATEOAS standards using Java based Katharsis
Keith Moore
 
Spring mvc
Harshit Choudhary
 
MVC 1.0 als alternative Webtechnologie
OPEN KNOWLEDGE GmbH
 
Single Page Applications: Your Browser is the OS!
Jeremy Likness
 
The Spring Update
Gunnar Hillert
 
Ajug - The Spring Update
Gunnar Hillert
 
Ad

Recently uploaded (20)

PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 

RESTful Web Services with Spring MVC

  • 1. RESTful Web Services With Spring MVC @DigitalSonic
  • 4. • How to identify Resources • How to use HTTP Verbs • How to design Representations • Miscellaneous • Cache/Version/HATEOAS Agenda
  • 6. • Domain Nouns • Collections & Composites • Functions • Controllers
  • 7. • 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
  • 8. Samples • Functions • http://domain/direction?from=shanghai&to=hangzhou • Controllers • http://domain/users/smith/address_merge
  • 9. Resource Granularity • Network Efficiency • Size of Representations • Client Convenience
  • 10. URL Mapping in Spring MVC • <mvc:annotation-driven/> • @RequestMapping • @PathVariable • @RequestParam • @MatrixVariable
  • 11. Building URI • UriComponents • UriComponentsBuilder • ServletUriComponentsBuilder
  • 12. 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
  • 14. 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.
  • 15. HTTP Method in Spring MVC • @RequestMapping • RequestMethod Enum
  • 16. HiddenHttpMethodFilter <input type="hidden" name="_method" value="put"/> Your web form doesn’t support so much verbs?
  • 18. • Different view technologies in Spring • JSP/JSTL (InternalResourceViewResolver) • Tiles (TilesViewResolver) • XML Marshalling (MarhsallingView) • JSON Mapping (MappingJacksonJsonView) • etc. • @ResponseBody
  • 19. HTML Should I say anything about this? You know what I mean.
  • 20. JSON • Jackson • MappingJacksonHttpMessageConverter • Jackson 2 • MappingJackson2HttpMessageConverter
  • 21. JSON • @RequestBody • @ResponseBody • @RequestMapping • consumes="application/json" • produces="application/json"
  • 22. JSON • Demo • Request • curl -H "Accept: application/json" http://localhost:8080/rest- demo/articles/10
  • 23. XML • JAXB 2 • Jaxb2RootElementHttpMessageConverter
  • 24. Links • Link • href/rel • Atom link Ref • self/alternate/related/edit • first/last/previous/next
  • 25. Content Negotiation • serval @RequestMapping with different produces • contentNegotiationManager • file extension • format parameter • Accept header • ContentNegotiatingViewResolver
  • 27. HTTP Status Codes • Use the RIGHT HTTP Status Code! • @ResponseStatus • http://www.ietf.org/rfc/rfc2616.txt
  • 28. 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
  • 30. Cache • Use the RIGHT Cache HTTP Headers • Cache-Control • Expires • Date • Last-Modified
  • 31. Cache for Static Resources
  • 32. Cache for Dynamic Resources
  • 33. Support for Etags • ShallowEtagHeaderFilter
  • 35. Version • Try to maintain the compatibility • URI • Representation • Link • If you can’t, then make a new version • subdomain • path segments • query parameters
  • 36. HATEOAS • Hypermedia as the Engine of Application State • Spring Hateoas • https://github.com/SpringSource/spring-hateoas • Better support for Links and Resources
  • 37. Demo
  • 39. • 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