Rest Api
Rest Api
Way 2:
1. Create new class which extends javax.ws.rs.core.Application;. This tells application
that it’s a jersey application.
2. It would scan all the packages as we have not specified any specific package
3. Url map to is defined by using following annotation in class created in step 1
a. @ApplicationPath("webapi")
Important Points:
1. Mark the class with @Path and provide path for your API
2. Application has method getClasses which can be extended to control which resources should be
considered.
3. Resources annotated with @Path, can also be marked as @Singleton. These resources will only
be initialized once for all requests.
@Path("/messages")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
Way to access parameters from client side
Query Params
Can you be used at method params or instance params.
@QueryParam("year") int year,
@QueryParam("start") int start,
@QueryParam("size") int size,
These parameters can be directly converted to specific primitive types as jersey knows its converter.
However if you want to convert it to some customer class you have defined, we need to define custom
classes
1. ParamConverterProvider
2. ParamConverter
Similar to ParamConvertProvider, there is a way for mapping the incoming message body or outgoing
response body to a specific custom class of your own. This can be done by implementing
1. MessageBodyWriter
2. MessageBodyReader
@Override
public Response toResponse(DataNotFoundException exception) {
ErrorMessage errorMsg = new ErrorMessage(exception.getMessage(),
404, "www.maitrey.com");
return Response.status(Status.NOT_FOUND)
.entity(errorMsg).build();
}}
Creating HATEOAS links for the related entities
// This is way, we can create path for sub resources.
// Template can accept the runtine values defined in your path
return uriInfo.getBaseUriBuilder()
.path(MessageResource.class)
.path(MessageResource.class, "getCommentResource" )
.resolveTemplate("messageId", message.getId())
.build()
.toString();
Content Negotiation
This is the concept where client can ask for specific content type for data returned from server. Server
can send the specific response based on content type required by client. If server does not support that
type of content, it would return error and if it does, it would send back data in requested content type.
Accept - text/xml
Based on content type requested, we can write two different methods (offcource it works for same
method as well.) to have different @produce and then return that type of data.
@Produces(value = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
This would come handy when we want to use different methods for different type of data. Like
following
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Message> getMessages()(
And following would return xml data for same method i.e. GET
@GET
@Produces(MediaType.APPLICATION_XML)
public List<Message> getMessagesXML()(
Same concept is applied to Consumes as well, where user can use different @Consumes for different
content type for data coming in.
Content-Type - application/json
Sending and handling REST request from Client side
Client client = ClientBuilder.newClient();