RESTEasy framework extensions ,GZIP,multi-part form -data
RESTEasy framework extensions
RESTEasy is another popular implementation of the JAX-RS specification available under the ASL (Apache) 2.0 license, which can run in any Servlet container. RESTEasy also comes with additional features on top of the plain JAX-RS functionalities.
To seamlessly integrate the RESTEasy features, the following Maven dependencies must be configured in the project in pom.xml under the dependencies element, as shown ahead:
In the following sections, let's take a look at some of the useful features provided by the RESTEasy framework
Caching using RESTEasy
REST APIs are being used for a majority of B2B interactions, and often, they are constrained by the strict performance obligations to deliver the agreed quality of service. Though network-level caching may be helpful to improve throughput, it does not fully address performance needs. In cases where the underlying state of the data is not prone to frequent changes, such as static data, and there is a significant load on the system, it becomes advantageous to use caching to improve the response time. To facilitate the implementation of caching for RESTful services, RESTEasy comes with the following features:
- Cache-control annotations for server-side caching
- Client-side caching
- Multipart content handling
Cache-control annotations
RESTEasy provides the @Cache and @NoCache annotations, following the JAX-RS specifications, with the @GET annotation for controlling the cache. These annotations can be applied at class and method levels.
The @NoCache annotation is used to specify when caching is not required; hence, the server needs to respond back with a fresh response for every request.
The @Cache annotation is used when the caching of a response is required. Given ahead is a brief list of attributes used to control the caching behavior. Take some time to refer to the previous chapter, which talks in detail about each of these attributes.
- maxAge: This indicates the maximum time the response message will remain in the cache.
- sMaxAge: This is the same as maxAge, but it applies for a proxy cache.
- noStore: This is used to avoid caching sensitive information set to true.
- mustRevalidate: If this is true, it revalidates the cache content and serves the fresh response.
- proxyRevalidate: This is the same as mustRevalidate, but it applies for a proxy cache.
- isPrivate: If this is true, the response messages will be cached for a single user only and will not be shared. If false, it means that the response messages can be cached by any cache.
Given ahead is the application of cache-control annotations for caching the response of the findDepartment function of DepartmentService:
In the preceding code snippet, when the client invokes the findDepartment function, the server will respond back with the response from the cache if it exists; otherwise, it will execute the function and return the response. In the latter case, the response will be cached as per the caching definition and will be used to serve future requests.
To set up the server-side cache, you must register an instance
of org.jboss.resteasy.plugins.cache.server.ServerCacheFeature via your application's getSingletons() or getClasses() method. The underlying cache is Infinispan. By default, RESTEasy will create an Infinispan cache for you.
Client-side caching
With server-side caching, the client will still have to hit the target resource. When the bandwidth is a constraint and there is a requirement to save network-round trips, it makes sense to enable caching at the client side when the consuming system has enough resources required for caching. The HTTP 1.1 protocol specification defines the guidelines around client-side caching with control parameters such as cache-control headers to decide about caching the server response and the cache-expiry mechanisms.
Following the HTTP specification, RESTEasy provides the BrowserCacheFeature API for implementing client-side caching, as shown ahead:
GZIP compression/decompression
With a lot of mobile applications being used in the market, it becomes essential to enable faster transmission of large chunks of data over a network. RESTEasy provides the GZIP compression/decompression feature. When the response body is plain text, it can be easily compressed on the server side and then decompressed on the client side. RESTEasy accomplishes this with the @GZIP annotation applied at the method level, as shown ahead:
The @GZIP annotation of RESTEasy takes care of compressing the response message body and similarly when a server receives a request with content-encoding equal to GZIP then the request message body is decompressed automatically.
Multipart content handling
Let's try with the simple use case of an employee uploading/downloading his/her profile picture. For this use case, we will create a simple HTML form for the employee to upload his/her profile picture, as shown ahead:
Please make sure that the form attribute, enctype, is specified as multipart/form-data. Once the user submits the form, the request is submitted to the EmployeeImageResoure class's addImageoperation. The org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput argument of the addImage operation will be used to read the submitted form contents. Similarly, to download the image, the user can use the getProfilePicture operation, which wraps the image file in the response using the javax.ws.rs.core.Response.ResponseBuilder class, as shown ahead:
Comments
Post a Comment