SlideShare uma empresa Scribd logo
1 de 51
Baixar para ler offline
Making Java REST
with JAX-RS 2.0
April 24, 2014	

Dmytro Chyzhykov
Disclaimer
The views and opinions expressed in this
presentation expressed by the speaker are solely his
own and do not necessary represent the views and
opinions of companies he is working or worked for.
3
Agenda
- What is REST?
- REST Principles and JAX-RS 2.0
- Q & A
4
HTTP
Response	

200 OKJSON
Media Type
Client ServerHTTP GET
Resources
Request http://example.com
Read more Hypertext Transfer Protocol -- HTTP/1.1
5
What is REST?
6
REST?
7
REST JEE7 Definition
REpresentational State Transfer is an
architectural style of client-server applications
centered around the transfer of representations
of resources through requests and responses.
Serves to build loosely coupled, lightweight web
services that are particularly well suited for creating
APIs for clients spread out across the Internet.
Was introduced and defined by Roy T. Fielding in
his doctoral dissertation.
Read more The Java EE 7 Tutorial: 29.1 What Are RESTful Web Services?
8
REST is...
- An architectural style, not a technology
- Everything is a Resource
- Suitable for CRUD (Create/Read/Update/Delete)
- Stateless by nature (excellent for distributed
systems)
- Cacheable (naturally supported by HTTP)
- Composable code on demand applications
9
REST Principles
- Give every thing its own ID
- Link things together (HATEOAS)
- Use standard HTTP methods
- Resources can have multiple representations 
- Communicate statelessly
- Support caching
10
Give every thing its own ID
11
Individual Resource ID
http://habrahabr.ru/users/theshade/!
https://twitter.com/fielding!
https://api.github.com/teams/46059!
Collection Resource ID
https://api.github.com/teams/!
https://twitter.com/fielding/followers!
https://api.github.com/user/repos?page=2!
http://ajax.googleapis.com/ajax/services/
search/web?v=1.0&q=Paris%20Hilton
12
JAXRS 2.0 resource example
GET http://locahost:8080/articles/7!
GET http://locahost:8080/articles/!
GET http://locahost:8080/articles/?page=1!
13
JAXRS 2.0 resource example
GET http://locahost:8080/articles/7!
GET http://locahost:8080/articles/!
GET http://locahost:8080/articles/?page=1!
@Path("articles")
public class ArticleResource {
}
14
JAXRS 2.0 resource example
GET http://locahost:8080/articles/7!
GET http://locahost:8080/articles/!
GET http://locahost:8080/articles/?page=1!
@Path("articles")
public class ArticleResource {
@GET
@Path("{id}")
public Article read(@PathParam("id") int id) {…}
}
15
JAXRS 2.0 resource example
GET http://locahost:8080/articles/7!
GET http://locahost:8080/articles/!
GET http://locahost:8080/articles/?page=1!
@Path("articles")
public class ArticleResource {
@GET
@Path("{id}")
public Article read(@PathParam("id") int id) {…}
@GET
public List<Article> list(@QueryParam("page")
@DefaultValue("1")
int page) {…}
}
16
Article POJO
public class Article {
private Integer id;
private String title;
private String content;
!
// Getters and setters go here
}
17
Let's try
$ curl -X GET http://localhost:8080/articles/7!
{!
"id": 7,!
"title": "REST Individual Resource",!
"content": "REST Individual Resource example"!
}
$ curl -X GET http://localhost:8080/articles/!
[!
{!
"id": 7,!
"title": "REST Individual Resource",!
"content": "REST Individual Resource example"!
}!
]
18
Link things together
19
HATEOAS
HATEOAS is abbreviation for Hypermedia as the
Engine of Application State.

A hypermedia-driven site provides information to
navigate the site's REST interfaces dynamically by
including hypermedia links with the responses.
Read more Why hypermedia APIs? and Understanding HATEOAS
20
Atom Links Style
<article id="123">
<title>Hypermedia and JAX-RS 2.0</title>
<content>JAX-RS 2.0 supports Hypermedia …</content>
!
<link rel="self" href=“/articles/123/" />
<link rel="update" href=“/articles/123/" />
<link rel="delete" href=“/articles/123/" />
<link rel="list" href=“/articles/“ />
!
</article>
Read more Atom Format Link Relation Extensions
21
Link Headers Style
HTTP/1.1 200 OK
Content-Type: application/xml
Link: </articles/123/>; rel=self;
Link: </articles/123/>; rel=update;
Link: </articles/123/>; rel=delete;
Link: </articles/>; rel=list;
!
<article id="123">
<title>Hypermedia and JAX-RS 2.0</title>
<content>JAX-RS 2.0 supports Hypermedia …</content>
</article>
Read more Web Linking:The Link Header Field
22
How to Imaging HATEOAS
23
How to Imaging HATEOAS
24
JAX-RS 2.0 HATEOAS
@POST
@Consumes({"application/json", "application/xml"})
@Produces({"application/json", "application/xml"})
public Response create(Article article) {
Article created = articleDao.create(article);
return Response
.ok(created)
.link("link-URI", “link-rel")
.links(produceLinks(created))
.build();
}
!
private Link[] produceLinks(Article article) {...}
Read more javax.ws.rs.core.Link
25
Jersey 2.x HATEOAS
public class Article {
@InjectLinks({
@InjectLink(resource = ArticleResource.class,
rel = "self", method = "read"),
@InjectLink(resource = ArticleResorce.class,
rel = "edit", method = "update"),
@InjectLink(resource = ArticleResource.class,
rel = "delete", method = "delete"),
@InjectLink(resource = ArticleResource.class,
rel = "list", method = “list")
})
private List<Link> links;
!
Read more Jersey 2.x Chapter 12. Declarative Hyperlinking
26
Let's try
$ curl -X GET “http://localhost:8080/articles/123"
{
"id": 123,
"title": "Hypermedia and JAX-RS 2.0",
"content": "JAX-RS 2.0 supports Hypermedia …",
"links": [
{ "uri": "/articles/",
"rel": "self" },
{ "uri": "/articles/",
"rel": "edit" },
{ "uri": "/articles/",
"rel": "delete" },
{ "uri": "/articles",
"rel": “list"}]
}
27
Use standard HTTP methods
Method Purpose
GET Read representation of the specified resource
POST Create or Update without a know ID
PUT Update or Create with a know ID
DELETE Remove
HEAD GET with no response, just metadata
OPTIONS Supported methods for the specified URL.
28
HTTP methods and JAX-RS 2.0
Method Annotation
POST @POST
GET @GET
PUT @PUT
DELETE @DELETE
HEAD @HEAD
OPTIONS @OPTIONS
29
Multiple Representations
30
Content Negotiation
GET http://example.com/stuff
Accept: application/xml, application/json, text/*
Give me resource contents as (in priority):
1. XML
2. or JSON
3. or any text content type you can
4. or return 406 Not Acceptable Error 

otherwise
31
Content Negotiation Example
curl -X POST "http://localhost:8080/articles" 
-H "Content-Type: application/xml" 
-d "<article> 
<title>Conneg and JAX-RS 2.0</title> 
<content> 
JAX-RS 2.0 supports Conneg… 
</content> 
</article>" 
-H "Accept: application/json"
{
"id": 7,
"title": "Conneg and JAX-RS 2.0",
"content": "JAX-RS 2.0 supports Conneg …"
}
32
Content Negotiation Example
curl -X POST "http://localhost:8080/articles" 
-H "Content-Type: application/xml" 
-d "<article> 
<title>Conneg and JAX-RS 2.0</title> 
<content> 
JAX-RS 2.0 supports Conneg… 
</content> 
</article>" 
-H "Accept: application/json"
{
"id": 7,
"title": "Conneg and JAX-RS 2.0",
"content": "JAX-RS 2.0 supports Conneg …"
}
33
Content Negotiation Example
@POST
@Consumes({"application/json", "application/xml"})
@Produces({"application/json", "application/xml"})
public Article create(Article article) {
return articleDao.create(article);
}
34
Language Negotiation
GET http://example.com/stuff
Accept-Language: en-us, en, ru
Encoding Negotiation
GET http://example.com/stuff
Accept-Encoding: gzip, deflate
35
Communicate Statelessly
36
State is Bad
Client
Load
Balancer
Server 1
Sessions
Server 3
Sessions
Server 2
Sessions
37
State is Bad
Client
Load
Balancer
Server 1
Sessions
Server 3
Sessions
Server 2
Sessions
Replication
Replication
38
State is Bad
Client
Load
Balancer
Server 1
Sessions
Server 3
Sessions
Server 2
Sessions
Replication
Replication
ItD
oesN
otScale 39
Communicate statelessly
Client
Load
Balancer
Server 1
Server 3
Server 2 Token	

Storage
40
Caching Support
“The best requests are those
that not even reach me”
- Anonymous overloaded Server
41
Caching Benefits
- Reduce bandwidth
- Reduce latency
- Reduce load on servers
- Hide network failures
42
Caching
Client	

Client

Cache	

Proxy

Cache	

Server
Reverse

Proxy

Cache	

Client	

Client

Cache	

Client	

Client

Cache	

43
HTTP Caching
GET http://example.com/stuff
!
< HTTP/1.1 200 OK
< Cache-Control: max-age=60
Cache-Control: max-age=<delta-seconds>

| s-max-age=<delta-seconds>

| no-cache

| no-store

| …
Read more Caching in HTTP
44
JAX-RS 2.0 Cache Control
Directive Usage
max-age cache.setMaxAge(int maxAge)
s-max-age cache.setSMaxAge(int maxAge)
no-cache cache.setNoCache(boolean noCache)
no-store cache.setNoStore(boolean noStore)
… …
CacheControl cache = new CacheControl();
Read more javax.ws.rs.core.CacheControl
45
Caching and JAX-RS 2.0
@GET
@Path("{id}")
public Response read(@PathParam("id") int id) {
Article article = articleDao.findById(id);
CacheControl cacheControl = new CacheControl();
cacheControl.setMaxAge(60);
!
return Response.ok(article)
.cacheControl(cacheControl)
.build();
}
46
Let's try
curl -X GET "http://localhost:8081/rest/articles/8"
-H "Accept: application/json" -v
< HTTP/1.1 200 OK
< Cache-Control: max-age=60
< Content-Type: application/json
<
{
"id": 8,
"title": "HTTP Caching and JAX-RS 2.0",
"content": "JAX-RS 2.0 supports HTTP Caching"
}
47
Wrapping Up
JAX-RS 2.0 is a POJO-based HTTP-centric
annotation-driven specification for for RESTful Web
Services.

Makes the developer focus on URLs, HTTP
methods and Media Types.
Implementations:
- Apache CXF
- Jersey
- RESTeasy
- Restlet
- others
48
Q & A
49
Thank you!
50
Старший Разработчик
Киноплатформы
Дмитрий Чижиков
dmytro.chyzhykov@yandex.ru
ffbit

Mais conteúdo relacionado

Mais procurados

Using Java to implement RESTful Web Services: JAX-RS
Using Java to implement RESTful Web Services: JAX-RSUsing Java to implement RESTful Web Services: JAX-RS
Using Java to implement RESTful Web Services: JAX-RSKatrien Verbert
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web servicesnbuddharaju
 
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey IntroductionseyCwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey Introductionseyelliando dias
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座Li Yi
 
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 EXAMPLEreneechemel
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPAShaun Smith
 
Introduction to RESTful Webservices in JAVA
Introduction to RESTful Webservices  in JAVA Introduction to RESTful Webservices  in JAVA
Introduction to RESTful Webservices in JAVA psrpatnaik
 
Json to hive_schema_generator
Json to hive_schema_generatorJson to hive_schema_generator
Json to hive_schema_generatorPayal Jain
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection apiMatthieu Aubry
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web ServicesFelipe Dornelas
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHPZoran Jeremic
 
REST - Representational state transfer
REST - Representational state transferREST - Representational state transfer
REST - Representational state transferTricode (part of Dept)
 
Representational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOASRepresentational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOASGuy K. Kloss
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India
 

Mais procurados (20)

Using Java to implement RESTful Web Services: JAX-RS
Using Java to implement RESTful Web Services: JAX-RSUsing Java to implement RESTful Web Services: JAX-RS
Using Java to implement RESTful Web Services: JAX-RS
 
Introduction to JAX-RS
Introduction to JAX-RSIntroduction to JAX-RS
Introduction to JAX-RS
 
Overview of RESTful web services
Overview of RESTful web servicesOverview of RESTful web services
Overview of RESTful web services
 
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey IntroductionseyCwinters Intro To Rest And JerREST and Jersey Introductionsey
Cwinters Intro To Rest And JerREST and Jersey Introductionsey
 
RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座RESTful SOA - 中科院暑期讲座
RESTful SOA - 中科院暑期讲座
 
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
 
RESTful services with JAXB and JPA
RESTful services with JAXB and JPARESTful services with JAXB and JPA
RESTful services with JAXB and JPA
 
Introduction to RESTful Webservices in JAVA
Introduction to RESTful Webservices  in JAVA Introduction to RESTful Webservices  in JAVA
Introduction to RESTful Webservices in JAVA
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Json to hive_schema_generator
Json to hive_schema_generatorJson to hive_schema_generator
Json to hive_schema_generator
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Easy rest service using PHP reflection api
Easy rest service using PHP reflection apiEasy rest service using PHP reflection api
Easy rest service using PHP reflection api
 
Introduction to RESTful Web Services
Introduction to RESTful Web ServicesIntroduction to RESTful Web Services
Introduction to RESTful Web Services
 
Consuming RESTful services in PHP
Consuming RESTful services in PHPConsuming RESTful services in PHP
Consuming RESTful services in PHP
 
REST - Representational state transfer
REST - Representational state transferREST - Representational state transfer
REST - Representational state transfer
 
Representational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOASRepresentational State Transfer (REST) and HATEOAS
Representational State Transfer (REST) and HATEOAS
 
OAuth: Trust Issues
OAuth: Trust IssuesOAuth: Trust Issues
OAuth: Trust Issues
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jerseyEclipse Day India 2015 - Rest with Java (jax rs) and jersey
Eclipse Day India 2015 - Rest with Java (jax rs) and jersey
 
Rest and Rails
Rest and RailsRest and Rails
Rest and Rails
 

Destaque

JAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web ServicesJAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web ServicesArun Gupta
 
Java web services using JAX-WS
Java web services using JAX-WSJava web services using JAX-WS
Java web services using JAX-WSIndicThreads
 
What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)Rudy De Busscher
 
Hypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASHypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASVladimir Tsukur
 
What's new in the Java API for JSON Binding
What's new in the Java API for JSON BindingWhat's new in the Java API for JSON Binding
What's new in the Java API for JSON BindingDmitry Kornilov
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplateGuo Albert
 
Criando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphere
Criando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphereCriando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphere
Criando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphereJuliano Martins
 
2015 UJUG, Servlet 4.0 portion
2015 UJUG, Servlet 4.0 portion2015 UJUG, Servlet 4.0 portion
2015 UJUG, Servlet 4.0 portionmnriem
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsMatt Stine
 
A first Draft to Java Configuration
A first Draft to Java ConfigurationA first Draft to Java Configuration
A first Draft to Java ConfigurationAnatole Tresch
 
Web protocols for java developers
Web protocols for java developersWeb protocols for java developers
Web protocols for java developersPavel Bucek
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Federico Paparoni
 
JPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsJPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsosa_ora
 
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute InfodeckServlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute InfodeckEdward Burns
 
Secure REST with JAX-RS
Secure REST with JAX-RSSecure REST with JAX-RS
Secure REST with JAX-RSCarol McDonald
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepGuo Albert
 
REST API design and construction with Java EE - pages from my work diary
REST API design and construction with Java EE - pages from my work diaryREST API design and construction with Java EE - pages from my work diary
REST API design and construction with Java EE - pages from my work diaryVineet Reynolds
 

Destaque (20)

JAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web ServicesJAX-RS 2.0: RESTful Web Services
JAX-RS 2.0: RESTful Web Services
 
Java web services using JAX-WS
Java web services using JAX-WSJava web services using JAX-WS
Java web services using JAX-WS
 
Web Services Tutorial
Web Services TutorialWeb Services Tutorial
Web Services Tutorial
 
Web servers
Web serversWeb servers
Web servers
 
What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)What is tackled in the Java EE Security API (Java EE 8)
What is tackled in the Java EE Security API (Java EE 8)
 
Hypermedia APIs and HATEOAS
Hypermedia APIs and HATEOASHypermedia APIs and HATEOAS
Hypermedia APIs and HATEOAS
 
What's new in the Java API for JSON Binding
What's new in the Java API for JSON BindingWhat's new in the Java API for JSON Binding
What's new in the Java API for JSON Binding
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
 
Criando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphere
Criando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphereCriando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphere
Criando um Web Service Restful com Jersey, Eclipse, JBoss, Tomcat, WebSphere
 
2015 UJUG, Servlet 4.0 portion
2015 UJUG, Servlet 4.0 portion2015 UJUG, Servlet 4.0 portion
2015 UJUG, Servlet 4.0 portion
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
 
A first Draft to Java Configuration
A first Draft to Java ConfigurationA first Draft to Java Configuration
A first Draft to Java Configuration
 
Web protocols for java developers
Web protocols for java developersWeb protocols for java developers
Web protocols for java developers
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
 
Overview of web services
Overview of web servicesOverview of web services
Overview of web services
 
JPA 2.1 performance tuning tips
JPA 2.1 performance tuning tipsJPA 2.1 performance tuning tips
JPA 2.1 performance tuning tips
 
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute InfodeckServlet 4.0 Adopt-a-JSR 10 Minute Infodeck
Servlet 4.0 Adopt-a-JSR 10 Minute Infodeck
 
Secure REST with JAX-RS
Secure REST with JAX-RSSecure REST with JAX-RS
Secure REST with JAX-RS
 
Spring + JPA + DAO Step by Step
Spring + JPA + DAO Step by StepSpring + JPA + DAO Step by Step
Spring + JPA + DAO Step by Step
 
REST API design and construction with Java EE - pages from my work diary
REST API design and construction with Java EE - pages from my work diaryREST API design and construction with Java EE - pages from my work diary
REST API design and construction with Java EE - pages from my work diary
 

Semelhante a Making Java REST with JAX-RS 2.0

RESTful Web services using JAX-RS
RESTful Web services using JAX-RSRESTful Web services using JAX-RS
RESTful Web services using JAX-RSArun Gupta
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSSpark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSArun Gupta
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and ODataAnil Allewar
 
Rest with java (jax rs) and jersey and swagger
Rest with java (jax rs) and jersey and swaggerRest with java (jax rs) and jersey and swagger
Rest with java (jax rs) and jersey and swaggerKumaraswamy M
 
The JSON REST API for WordPress
The JSON REST API for WordPressThe JSON REST API for WordPress
The JSON REST API for WordPressTaylor Lovett
 
REST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practiceREST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practicehamnis
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareAlona Mekhovova
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasyJBug Italy
 
Felix HTTP - Paving the road to the future
Felix HTTP - Paving the road to the futureFelix HTTP - Paving the road to the future
Felix HTTP - Paving the road to the futureMarcel Offermans
 
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonJAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonArun Gupta
 
Building Restful Web Services with Java
Building Restful Web Services with JavaBuilding Restful Web Services with Java
Building Restful Web Services with JavaVassil Popovski
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011Shreedhar Ganapathy
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface pptTaha Malampatti
 
JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013Jagadish Prasath
 
Java serverpages
Java serverpagesJava serverpages
Java serverpagesAmit Kumar
 
Couchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemCouchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemdelagoya
 
Building Beautiful REST APIs in ASP.NET Core
Building Beautiful REST APIs in ASP.NET CoreBuilding Beautiful REST APIs in ASP.NET Core
Building Beautiful REST APIs in ASP.NET CoreStormpath
 

Semelhante a Making Java REST with JAX-RS 2.0 (20)

RESTful Web services using JAX-RS
RESTful Web services using JAX-RSRESTful Web services using JAX-RS
RESTful Web services using JAX-RS
 
Spark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RSSpark IT 2011 - Developing RESTful Web services with JAX-RS
Spark IT 2011 - Developing RESTful Web services with JAX-RS
 
JAX-RS 2.0 and OData
JAX-RS 2.0 and ODataJAX-RS 2.0 and OData
JAX-RS 2.0 and OData
 
Rest with java (jax rs) and jersey and swagger
Rest with java (jax rs) and jersey and swaggerRest with java (jax rs) and jersey and swagger
Rest with java (jax rs) and jersey and swagger
 
The JSON REST API for WordPress
The JSON REST API for WordPressThe JSON REST API for WordPress
The JSON REST API for WordPress
 
REST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practiceREST teori og praksis; REST in theory and practice
REST teori og praksis; REST in theory and practice
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 
Using and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middlewareUsing and scaling Rack and Rack-based middleware
Using and scaling Rack and Rack-based middleware
 
May 2010 - RestEasy
May 2010 - RestEasyMay 2010 - RestEasy
May 2010 - RestEasy
 
Felix HTTP - Paving the road to the future
Felix HTTP - Paving the road to the futureFelix HTTP - Paving the road to the future
Felix HTTP - Paving the road to the future
 
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX LondonJAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
JAX-RS 2.0: New and Noteworthy in RESTful Web services API at JAX London
 
Building Restful Web Services with Java
Building Restful Web Services with JavaBuilding Restful Web Services with Java
Building Restful Web Services with Java
 
Android and REST
Android and RESTAndroid and REST
Android and REST
 
JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011JAX-RS JavaOne Hyderabad, India 2011
JAX-RS JavaOne Hyderabad, India 2011
 
Request dispacther interface ppt
Request dispacther interface pptRequest dispacther interface ppt
Request dispacther interface ppt
 
JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013JAX RS 2.0 - OTN Bangalore 2013
JAX RS 2.0 - OTN Bangalore 2013
 
Java serverpages
Java serverpagesJava serverpages
Java serverpages
 
Data in RDF
Data in RDFData in RDF
Data in RDF
 
Couchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problemCouchdb: No SQL? No driver? No problem
Couchdb: No SQL? No driver? No problem
 
Building Beautiful REST APIs in ASP.NET Core
Building Beautiful REST APIs in ASP.NET CoreBuilding Beautiful REST APIs in ASP.NET Core
Building Beautiful REST APIs in ASP.NET Core
 

Último

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Último (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

Making Java REST with JAX-RS 2.0

  • 1.
  • 2. Making Java REST with JAX-RS 2.0 April 24, 2014 Dmytro Chyzhykov
  • 3. Disclaimer The views and opinions expressed in this presentation expressed by the speaker are solely his own and do not necessary represent the views and opinions of companies he is working or worked for. 3
  • 4. Agenda - What is REST? - REST Principles and JAX-RS 2.0 - Q & A 4
  • 5. HTTP Response 200 OKJSON Media Type Client ServerHTTP GET Resources Request http://example.com Read more Hypertext Transfer Protocol -- HTTP/1.1 5
  • 8. REST JEE7 Definition REpresentational State Transfer is an architectural style of client-server applications centered around the transfer of representations of resources through requests and responses. Serves to build loosely coupled, lightweight web services that are particularly well suited for creating APIs for clients spread out across the Internet. Was introduced and defined by Roy T. Fielding in his doctoral dissertation. Read more The Java EE 7 Tutorial: 29.1 What Are RESTful Web Services? 8
  • 9. REST is... - An architectural style, not a technology - Everything is a Resource - Suitable for CRUD (Create/Read/Update/Delete) - Stateless by nature (excellent for distributed systems) - Cacheable (naturally supported by HTTP) - Composable code on demand applications 9
  • 10. REST Principles - Give every thing its own ID - Link things together (HATEOAS) - Use standard HTTP methods - Resources can have multiple representations - Communicate statelessly - Support caching 10
  • 11. Give every thing its own ID 11
  • 12. Individual Resource ID http://habrahabr.ru/users/theshade/! https://twitter.com/fielding! https://api.github.com/teams/46059! Collection Resource ID https://api.github.com/teams/! https://twitter.com/fielding/followers! https://api.github.com/user/repos?page=2! http://ajax.googleapis.com/ajax/services/ search/web?v=1.0&q=Paris%20Hilton 12
  • 13. JAXRS 2.0 resource example GET http://locahost:8080/articles/7! GET http://locahost:8080/articles/! GET http://locahost:8080/articles/?page=1! 13
  • 14. JAXRS 2.0 resource example GET http://locahost:8080/articles/7! GET http://locahost:8080/articles/! GET http://locahost:8080/articles/?page=1! @Path("articles") public class ArticleResource { } 14
  • 15. JAXRS 2.0 resource example GET http://locahost:8080/articles/7! GET http://locahost:8080/articles/! GET http://locahost:8080/articles/?page=1! @Path("articles") public class ArticleResource { @GET @Path("{id}") public Article read(@PathParam("id") int id) {…} } 15
  • 16. JAXRS 2.0 resource example GET http://locahost:8080/articles/7! GET http://locahost:8080/articles/! GET http://locahost:8080/articles/?page=1! @Path("articles") public class ArticleResource { @GET @Path("{id}") public Article read(@PathParam("id") int id) {…} @GET public List<Article> list(@QueryParam("page") @DefaultValue("1") int page) {…} } 16
  • 17. Article POJO public class Article { private Integer id; private String title; private String content; ! // Getters and setters go here } 17
  • 18. Let's try $ curl -X GET http://localhost:8080/articles/7! {! "id": 7,! "title": "REST Individual Resource",! "content": "REST Individual Resource example"! } $ curl -X GET http://localhost:8080/articles/! [! {! "id": 7,! "title": "REST Individual Resource",! "content": "REST Individual Resource example"! }! ] 18
  • 20. HATEOAS HATEOAS is abbreviation for Hypermedia as the Engine of Application State.
 A hypermedia-driven site provides information to navigate the site's REST interfaces dynamically by including hypermedia links with the responses. Read more Why hypermedia APIs? and Understanding HATEOAS 20
  • 21. Atom Links Style <article id="123"> <title>Hypermedia and JAX-RS 2.0</title> <content>JAX-RS 2.0 supports Hypermedia …</content> ! <link rel="self" href=“/articles/123/" /> <link rel="update" href=“/articles/123/" /> <link rel="delete" href=“/articles/123/" /> <link rel="list" href=“/articles/“ /> ! </article> Read more Atom Format Link Relation Extensions 21
  • 22. Link Headers Style HTTP/1.1 200 OK Content-Type: application/xml Link: </articles/123/>; rel=self; Link: </articles/123/>; rel=update; Link: </articles/123/>; rel=delete; Link: </articles/>; rel=list; ! <article id="123"> <title>Hypermedia and JAX-RS 2.0</title> <content>JAX-RS 2.0 supports Hypermedia …</content> </article> Read more Web Linking:The Link Header Field 22
  • 23. How to Imaging HATEOAS 23
  • 24. How to Imaging HATEOAS 24
  • 25. JAX-RS 2.0 HATEOAS @POST @Consumes({"application/json", "application/xml"}) @Produces({"application/json", "application/xml"}) public Response create(Article article) { Article created = articleDao.create(article); return Response .ok(created) .link("link-URI", “link-rel") .links(produceLinks(created)) .build(); } ! private Link[] produceLinks(Article article) {...} Read more javax.ws.rs.core.Link 25
  • 26. Jersey 2.x HATEOAS public class Article { @InjectLinks({ @InjectLink(resource = ArticleResource.class, rel = "self", method = "read"), @InjectLink(resource = ArticleResorce.class, rel = "edit", method = "update"), @InjectLink(resource = ArticleResource.class, rel = "delete", method = "delete"), @InjectLink(resource = ArticleResource.class, rel = "list", method = “list") }) private List<Link> links; ! Read more Jersey 2.x Chapter 12. Declarative Hyperlinking 26
  • 27. Let's try $ curl -X GET “http://localhost:8080/articles/123" { "id": 123, "title": "Hypermedia and JAX-RS 2.0", "content": "JAX-RS 2.0 supports Hypermedia …", "links": [ { "uri": "/articles/", "rel": "self" }, { "uri": "/articles/", "rel": "edit" }, { "uri": "/articles/", "rel": "delete" }, { "uri": "/articles", "rel": “list"}] } 27
  • 28. Use standard HTTP methods Method Purpose GET Read representation of the specified resource POST Create or Update without a know ID PUT Update or Create with a know ID DELETE Remove HEAD GET with no response, just metadata OPTIONS Supported methods for the specified URL. 28
  • 29. HTTP methods and JAX-RS 2.0 Method Annotation POST @POST GET @GET PUT @PUT DELETE @DELETE HEAD @HEAD OPTIONS @OPTIONS 29
  • 31. Content Negotiation GET http://example.com/stuff Accept: application/xml, application/json, text/* Give me resource contents as (in priority): 1. XML 2. or JSON 3. or any text content type you can 4. or return 406 Not Acceptable Error 
 otherwise 31
  • 32. Content Negotiation Example curl -X POST "http://localhost:8080/articles" -H "Content-Type: application/xml" -d "<article> <title>Conneg and JAX-RS 2.0</title> <content> JAX-RS 2.0 supports Conneg… </content> </article>" -H "Accept: application/json" { "id": 7, "title": "Conneg and JAX-RS 2.0", "content": "JAX-RS 2.0 supports Conneg …" } 32
  • 33. Content Negotiation Example curl -X POST "http://localhost:8080/articles" -H "Content-Type: application/xml" -d "<article> <title>Conneg and JAX-RS 2.0</title> <content> JAX-RS 2.0 supports Conneg… </content> </article>" -H "Accept: application/json" { "id": 7, "title": "Conneg and JAX-RS 2.0", "content": "JAX-RS 2.0 supports Conneg …" } 33
  • 34. Content Negotiation Example @POST @Consumes({"application/json", "application/xml"}) @Produces({"application/json", "application/xml"}) public Article create(Article article) { return articleDao.create(article); } 34
  • 35. Language Negotiation GET http://example.com/stuff Accept-Language: en-us, en, ru Encoding Negotiation GET http://example.com/stuff Accept-Encoding: gzip, deflate 35
  • 37. State is Bad Client Load Balancer Server 1 Sessions Server 3 Sessions Server 2 Sessions 37
  • 38. State is Bad Client Load Balancer Server 1 Sessions Server 3 Sessions Server 2 Sessions Replication Replication 38
  • 39. State is Bad Client Load Balancer Server 1 Sessions Server 3 Sessions Server 2 Sessions Replication Replication ItD oesN otScale 39
  • 41. Caching Support “The best requests are those that not even reach me” - Anonymous overloaded Server 41
  • 42. Caching Benefits - Reduce bandwidth - Reduce latency - Reduce load on servers - Hide network failures 42
  • 44. HTTP Caching GET http://example.com/stuff ! < HTTP/1.1 200 OK < Cache-Control: max-age=60 Cache-Control: max-age=<delta-seconds>
 | s-max-age=<delta-seconds>
 | no-cache
 | no-store
 | … Read more Caching in HTTP 44
  • 45. JAX-RS 2.0 Cache Control Directive Usage max-age cache.setMaxAge(int maxAge) s-max-age cache.setSMaxAge(int maxAge) no-cache cache.setNoCache(boolean noCache) no-store cache.setNoStore(boolean noStore) … … CacheControl cache = new CacheControl(); Read more javax.ws.rs.core.CacheControl 45
  • 46. Caching and JAX-RS 2.0 @GET @Path("{id}") public Response read(@PathParam("id") int id) { Article article = articleDao.findById(id); CacheControl cacheControl = new CacheControl(); cacheControl.setMaxAge(60); ! return Response.ok(article) .cacheControl(cacheControl) .build(); } 46
  • 47. Let's try curl -X GET "http://localhost:8081/rest/articles/8" -H "Accept: application/json" -v < HTTP/1.1 200 OK < Cache-Control: max-age=60 < Content-Type: application/json < { "id": 8, "title": "HTTP Caching and JAX-RS 2.0", "content": "JAX-RS 2.0 supports HTTP Caching" } 47
  • 48. Wrapping Up JAX-RS 2.0 is a POJO-based HTTP-centric annotation-driven specification for for RESTful Web Services.
 Makes the developer focus on URLs, HTTP methods and Media Types. Implementations: - Apache CXF - Jersey - RESTeasy - Restlet - others 48