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 javax.ws.rs.GET;
4  import javax.ws.rs.POST;
5  import javax.ws.rs.Path;
6  import javax.ws.rs.core.MediaType;
7  import javax.ws.rs.core.Response;
8  
9  @Path("webresource1")
10 public class WebResource1 {
11 
12  @GET
13  public Response getMethod() {
14      Response response = Response.ok(Boolean.TRUE, MediaType.TEXT_PLAIN).build();
15      return response;
16  }
17  
18  @POST
19  public Response postMethod() {
20      Response response = Response.ok(Boolean.FALSE, MediaType.TEXT_PLAIN).build();
21      return response;
22  }
23 }

We give a unique name to the resource.

This unique name also becomes the part of the complete URL used to access the resource. This is achieved by passing the unique name as argument to @Path annotation. Refer to line 9.

The @Path annotation can be applied at method level also. If applied at method level, we need to atleast annotate the class with @Path(“/”).

Next we provide methods to handle different HTTP request types used to access the resource, in this case they are GET and POST.

The methods are “getMethod” and “postMethod”.

We annotate the “getMethod” with @GET to indicate the server that the method will handle get requests.

We also annotate the “postMethod” with @POST to indicate the server that the method will handle post requests.

Both method returns an instance of Response class.

In our example we create an instance of Response by using static method “ok” and passing two arguments
1) the value
2) media type of the response being created.

Next we create web.xml with the following entry

web.xml

1 <?xml version=”1.0″ encoding=”UTF-8″?>
2 <web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns=”http://xmlns.jcp.org/xml/ns/javaee&#8221; xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&#8221; id=”WebApp_ID” version=”3.1″>
3 <servlet>
4 <servlet-name>javax.ws.rs.core.Application</servlet-name>
5 </servlet>
6 <servlet-mapping>
7 <servlet-name>javax.ws.rs.core.Application</servlet-name>
8 <url-pattern>/myresources/*</url-pattern>
9 </servlet-mapping>
10</web-app>

We create a war file named “JAXRSServerConcepts.war” with the compiled class file of WebResource1 under
JAXRSServerConcepts\WEB-INF\classes\resource

and

web.xml under JAXRSServerConcepts\WEB-INF

When the war file is deployed, the application server which implements the interface javax.ws.rs.core.Application scans for classes with @Path annotations,

The application server adds the scanned classes as a JAX-RS resource, and maps the URL to the resource.

Due to mapping created by application server, the server knows how to direct the request containing the url to appropriate resource.

The URL generated in this example will be

http://localhost:8080/myresources/webresource1

When the resource is requested using the above url, myresources in the url will tell the server to use the servlet javax.ws.rs.core.Application.

The javax.ws.rs.core.Application uses webresource1 in the url to map to the WebResource1 class.

Based on the http request type, appropriate method in WebResource1 instance is called.

Output

Open a rest client,
1) choose the GET http request type and type the url you will get simple text true.
2) choose the POST http request type and type the url you will get simple text false.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s