This post continues the studies about Rest Service already talked in my previous post.
The Rest is a stateless service which can access resources using methods (GET, POST, DELETE, PUT, etc) based on request/response communication.
The Rest is a client/server architecture implemented by some API such as Jersey and RESTEasy.
Now, you can see an exemple of a controller that use an entity with two attributes. The complete project used on this post you can see here.
This code is a simple example of a rest that use GET and POST methods. The code has methods to show different scenarios: with parameters, without return, with common return and with a specific return. Furthermore, you can see the (1) @Path annotation, relative the the URI of the resource, (2) @Produces, which indicate the MIME media type of the return sent back to the client, and (3) the @Consumes, relative the MIME media type that will be consumed by the service. If @Produces or @Consume are applied at the class level, all the methods will have this value by default. However, the methods have priority. If no annotation is used then the method can accept any media type.
To access the methods you can use the Postman tool. Also, to test the GET method you can access directly by your browser: http://localhost:8080/<name_your_application>/rest/users
And the post you can create a class to run with the method:
Providers
The providers are resources to map the Http Request and Http Response. The providers are MessageBodyReader (MBR) and MessageBodyWriter (MBW). The MBR map the request to the method parameter. The MBW map the return value to the response body.
MBR:“conversion of HTTP request content to a Java type” [1]
MBW:“transforms a Java type to an on-wire format to be returned to the client” [2]
Now, let’s see an example to MBW. It’ll map the response.
"works in tandem with (intercept) Message Body Readers and Writers"
"used to manipulate HTTP message payloads"
Let's see below an ReaderInterceptor. Same as MessageBodyReader, this provider is called to process before the method start.
And now, a WriterInterceptor. As well as MessageBodyWriter, this provider is called to process the response, after the method finish.
The console after run ...
GET
After run the first GET method, no provider is called. It was not necessary. It is a method with a simple parameter and no return inside the response.
The second GET method has a simple return, already known by rest API (text/html). Then, only one provider is called.
The third GET has a return inside the response of a specific entity. It needs to be translate. Because of that, now is called the writers.
POST
On the first post method, a specific parameter is given to the method and no return to the response. Because of that, only reader providers are needed.
And the last scenario the method receive an entity as parameter and return an entity. So, readers and writers are called.
Filters
Interceptors work with message bodies and filters workt with request and response.
Filters are mainly used to modify or process incoming and outgoing request headers or response headers. They execute before and after request and response processing. [4]
There are two types of filter: Server Side and Client Side. Both can be customized implementing their interfaces. They can be used to handle the log of the services.
Server Side Interfaces:
ContainerRequestFilters: run before your JAX-RS resource method is invoked.[4]
ContainerResponseFilters: run after your JAX-RS resource method is invoked.[4]
Client Side:
ClientRequestFilter: run before your HTTP request is sent over the wire to the server.[4]
ClientResponseFilter: run after a response is received from the server, but before the response body is unmarshalled.[4]
The order of execution is:
- Server Filter Request
- - Client Filter Request
- - Client Filter Response
- Server Filter Response
The ContainerRequestFilters can be used with @PreMatching or @PostMatching annotation. These are resources to change request attributes before JAX-RS.
An example of code to use filter you can see in [5] or [6]
Pipeline
For all the process, since start the method to return the result, you can apply filters and interceptors. Below you see two pictures that I believe give a good view to us about this subject.