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.
package com.concretepage.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
public class WelcomeRest {
	public String getWelcomeMsg(@PathParam("name") String name){
		 return "Welcome "+name;
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
	<display-name>JAX RS Application</display-name>
	  <servlet-name>REST Service</servlet-name>
	  <servlet-name>REST Service</servlet-name>
To test the JAX-RS Web Service, you can use the URI as below.

The below are the dependencies for JAX-RS Web Service.

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.
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);
Find the jar dependency for JAX-RS client.

Find the Eclipse Configuration for JAX-RS Client.

Example of RESTful Web Services with JAX-RS

Download Source Code


©2022 concretepage.com | Privacy Policy | Contact Us