This post explains how to create filters in JAX-RS code. The filters can be placed before and after the web resource. So we can use the filters for 1) modifying the request 2) restricting certain requests from reaching the webresource 3) modifying the response In the below code I have created a custom filter, which…… Continue reading Creating filters in JAX-RS
Category: JAX-RS
BeanParam annotation example
This post will explain the use of BeanParam annotation in JAX-RS with an example. The purpose of BeanParam annotation is to consolidate all the user input (i.e., input through QueryParam, HeaderParam, FormParam, etc) and inject into a given class instance variables. Below is an example of webresource whose only method “retrieveRecord1” has a parameter annotated…… Continue reading BeanParam annotation example
Mapping exceptions to Response in JAX-RS
In JAX-RS, we can map exceptions to a Response. This will be useful in hiding the exceptions from the client of JAX-RS web services. Below is an example showing how to map exceptions to appropriate responses. First we need to implement ExceptionMapper interface as shown below. Here we want to map NullPointer exception to Internal…… Continue reading Mapping exceptions to Response in JAX-RS
Retrieving url template parameters in JAX-RS annotated method
In JAX-RS, we can create url templates in the format as shown below http://localhost:8080/JAXRSConcepts/myresources/webresource4/retrieveRecord1/ {parameter_name} where anything between ‘{‘ and ‘}’ acts as places where we can provide parameters when creating the actual url. The name between ‘{‘ and ‘}’ in url template will act as the parameter name. In the actual url the text…… Continue reading Retrieving url template parameters in JAX-RS annotated method
Retrieving header parameters in JAX-RS annotated method
The post explains different ways of retrieving header parameters in JAX-RS annotated method. Below is the complete code 1 package resource; 2 3 import javax.ws.rs.GET; 4 import javax.ws.rs.HeaderParam; 5 import javax.ws.rs.Path; 6 import javax.ws.rs.core.Context; 7 import javax.ws.rs.core.HttpHeaders; 8 import javax.ws.rs.core.MediaType; 9 import javax.ws.rs.core.Response; 10 11 @Path(“webresource3”) 12 public class WebResource3 { 13 @GET 14 @Path(“headerParameters1”)…… Continue reading Retrieving header parameters in JAX-RS annotated method
Retrieving query parameters in JAX-RS annotated method
The post explains different ways of retrieving query parameters in JAX-RS annotated method. Below is the complete code 1 package resource; 2 3 import javax.ws.rs.GET; 4 import javax.ws.rs.Path; 5 import javax.ws.rs.QueryParam; 6 import javax.ws.rs.core.Context; 7 import javax.ws.rs.core.MediaType; 8 import javax.ws.rs.core.Response; 9 import javax.ws.rs.core.UriInfo; 10 11 @Path(“webresource2”) 12 public class WebResource2 { 13 @GET 14 @Path(“queryParameters1”)…… Continue reading Retrieving query parameters in JAX-RS annotated method
Binding single http request type to multiple web resource methods in JAX-RS Application
This post explains how to bind one type of http request to multiple methods of a web resource. Sometimes we need to design a web resource in such a way that we can bind two different yet related web url to different methods. 1) Different url meaning different in their purpose 2) Related url meaning…… Continue reading Binding single http request type to multiple web resource methods in JAX-RS Application
JAX-RS Simple Example
This post explains with an example how to create a simple JAX-RS application. We will create a web resource with two methods, one method to handle get requests and another method to handle post requests, as shown below The resource name is name of the class itself. WebResource Code 1 package resource; 2 3 import…… Continue reading JAX-RS Simple Example