7551207b9d54a4c7939f071ed1187189
7551207b9d54a4c7939f071ed1187189
7551207b9d54a4c7939f071ed1187189
by Jeevan
Jagadeesh
REpresentational State Transfer
(REST)
Definition of REST -
Resources
Every distinguishable entity is a resource. A resource may be a Web site,
an HTML page, an XML document, a Web service, a physical device, etc.
URIs to Identify Resources
Every resource is uniquely identified by a URL.
Resources
HTTP Standard Methods
Communicate with a standard set of methods
Uniform interface
Decoupled client-server interaction
Stateless
Cacheable
Layered
Code on Demand (optional)
Why is it called
"Representational State Transfer? "
Java has defined JAX-RS (JSR 311) as the Java API for
RESTful Web Services
Popular Java Rest
implementations
@javax.ws.rs.Produces
Used to specify the MIME media types of representations a
resource can produce and send back to the client
Can be applied at both the class and method levels
Method level overrides class level
@javax.ws.rs.Consumes
Used to specify the MIME media types of representations a
resource can consume
Can be applied at both the class and method levels
Method level override a class level
How it Works?
Scanning/Loading only the
required packages
web.xml
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.rest</param-value>
</init-param>
Demo
Sample Xml:
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<first-name>Jeevan</first-name>
<last-name>Jagadeesh</last-name>
<city>Bangalore</city>
</customer>
Test Scenario’s:
1. Create customers using POST method
2. Retrieve the customers using GET with id
3. Update a customer
4. Retrieve the updated customer
5. Delete a customer
6. Do a get on the deleted customer
Sub-Resource Locators
@javax.ws.rs.PathParam
@javax.ws.rs.MatrixParam
@javax.ws.rs.QueryParam
@javax.ws.rs.FormParam
@javax.ws.rs.HeaderParam
@javax.ws.rs.CookieParam
@PathParam
@Path("/customers")
public class CustomerResource {
@GET
public void getCustomers(@QueryParam("start") int start,
@QueryParam("end") int end) {
System.out.println("Start " + start);
System.out.println("End " + end);
}
}
URI - /customers?start=0&end=10
@MatrixParam
@MatrixParam annotation is used to inject name value pair in the URI path
into Java method
Matrix parameters must be separate by a semi colon
@GET
@Path("/matrix")
public Response testMatrixParam(@MatrixParam("firstName") String firstName,
@MatrixParam("lastName") String lastName) {
System.out.println("firstName " + firstName);
System.out.println("lastName" + lastName);
return Response.status(200).entity("In Matrix Param firstName: " +firstName + " lastName:
"+lastName).build();
}
URI - matrix;firstName=Jeevan;lastName=Jagadeesh
@FormParam
return Response.status(200).entity(
"In Form Param, firstname : " + firstname + ", lastname : "
+ lastname).build();
}
URI - /customers/formParam
POST Method, content-type=application/x-www-form-urlencoded,
HTTP Body = firstname=Jeevan
lastName=Jagadeesh
@HeaderParam
Servers can store state information in cookies on the client, and can retrieve that
information when the client makes its next request
JAX RS and JAXB
@XmlRootElement(name = "customer")
public class Customer {
}
Successful Response:
Successful HTTP response code numbers range from 200 to 399
• GET returns 200 OK
• POST returns 201 CREATED
• 200 OK
• 201 Created
• 202 Accepted
• 203 Non-Authoritative Information
• 204 No Content
• 205 Reset Content
• 206 Partial Content
Error Response:
Error response code numbers range from 400 to 599
• 404 Not Found
• 405 Method Not allowed
• 406 Not Acceptable
Customizing Response with
Response Class
if (customer== null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
Custom Exception Handling
using ExceptionMapper
There could be various exceptions thrown from application code and third-
party frameworks. Catching and then wrapping all these exceptions within
WebApplicationException would become quite tedious
Alternatively we can implement javax.ws.rs.ext.ExceptionMapper for the
exception type you want to handle.
ExceptionMapper implementation class must be annotated with the
@Provider annotation
toResponse()method receives the thrown exception and creates a Response
object that will be used to build the HTTP response
Content Negotiation