Home  >  Web Services

Example of RESTful Web Services with JAX-RS

By Arvind Rai, November 18, 2013
On this page we will provide example of RESTful web services with JAX-RS using Jersey. REST is Representational State Transfer. REST works on some constraints like uniform interface and we get performance, scalability, and modifiability. RESTful web services have the best performance on the web and accessed via URI. RESTful Web Services uses stateless protocol like HTTP. RESTful Web Services are lightweight, simple and fast because of below usages.

a. Resource identification through URI
b. Uniform interface
c. Self-descriptive messages
d. Stateful interactions through hyperlinks

Now we will see how to create RESTful Web Services with JAX-RS and its client.

Creating Jersey RESTful Web Services with JAX-RS

RESTful web services classes are annotated with @Path. The methods are annotated with @GET, @PUT, @POST, or @DELETE. The response types are declared by @Produces. In our example we are using plain text. The arguments of the methods are annotated with @PathParam. In our example we are using Tomcat.
WelcomeRest.java
package com.concretepage.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@Path("/restwb") 
public class WelcomeRest {
	@GET
	@Path("/msg/{name}")
        @Produces("text/plain")
	public String getWelcomeMsg(@PathParam("name") String name){
		 return "Welcome "+name;
	}
} 
web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
	<display-name>JAX RS Application</display-name>
	<servlet>
	  <servlet-name>REST Service</servlet-name>
	<servlet-class>
	  com.sun.jersey.spi.container.servlet.ServletContainer
	</servlet-class>
	  <init-param>
	    <param-name>com.sun.jersey.config.property.packages</param-name>
	    <param-value>com.concretepage.rest</param-value>
	  </init-param>
	  <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	  <servlet-name>REST Service</servlet-name>
	  <url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app> 
To test the JAX-RS Web Service, you can use the URI as below.
http://localhost:8080/RestWB/restwb/msg/Ram

The below are the dependencies for JAX-RS Web Service.
<dependencies>
   <dependency>
		<groupId>javax.ws.rs</groupId>
		<artifactId>jsr311-api</artifactId>
		<version>1.1.1</version>
   </dependency>
   <dependency>
		<groupId>com.sun.jersey</groupId>
		<artifactId>jersey-server</artifactId>
		<version>1.8</version>
   </dependency>
</dependencies> 



Eclipse configuration of RESTful Web Services files.

Example of RESTful Web Services with JAX-RS

Creating Jersey Client for JAX-RS Web Services

We will use jersey API to run our client. DefaultClientConfig returns the instance of ClientConfig. To create client we pass the ClientConfig object to Client.create method. Our web service methods need the input parameter. We pass it by appending in URI and WebResource class achieves it.
RestWBClient.java
package com.concretepage.client;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class RestWBClient {
    static final String REST_URI = "http://localhost:8080/RestWB/restwb/";
    static final String MSG_PATH = "msg";
    public static void main(String[] args) {
    	ClientConfig config = new DefaultClientConfig();   
        Client client = Client.create(config);
        WebResource service = client.resource(REST_URI);
        WebResource msgService = service.path(MSG_PATH).path("Ram");
        String msg = msgService.accept(MediaType.TEXT_PLAIN).get(String.class);
        System.out.println(msg);
    }
} 
Find the jar dependency for JAX-RS client.
<dependencies>
	<dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.6</version>
    </dependency>
</dependencies> 

Find the Eclipse Configuration for JAX-RS Client.

Example of RESTful Web Services with JAX-RS

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2020 concretepage.com | Privacy Policy | Contact Us