Home  >  Web Services

JSON Response Using RESTful Web Service

By Arvind Rai, May 14, 2014
On this page we will learn how a RESTful web service will return a JSON response. To create server for RESTful Web Service, we are using RESTEasy and for client we are using Jersey. For server we need to use JAXB, JBoss @BadgerFish and media type as application/json. We need to create a bean which will represent an XML with the help of JAXB annotation like @XmlRootElement, @XmlAttribute and @XmlElement. The servlet which will fulfill the web service request and response is HttpServletDispatcher which will be configured in web.xml. We will see step by step how to implement RESTful Web Service to return JSON response.

RESTEasy HttpServletDispatcher in web.xml

In web.xml, we need to configure HttpServletDispatcher and resteasy.scan as true.
<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"

Create Class to Represent XML using JAXB

JSON data is converted from a bean. Bean is created using @XmlRootElement at class level.
package com.concretepage.rest;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
public class Student {
	private int id;
	private String name;
	private String collegeName;
	private int age;
        public int getId() {
		return id;
	public void setId(int id) {
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;
	public String getCollegeName() {
		return collegeName;
	public void setCollegeName(String collegeName) {
		this.collegeName = collegeName;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;
Find the description of XML annotation applied on class.
@XmlRootElement : It is annotated at class level and after annotation that class represents XML data.
@XmlAttribute : It enables a non-static property of class to map with a local element of XML Schema.
@XmlElement : It maps a non-static property to XML element.

Create RESTEasy Web Service to Produce JSON with @BadgerFish

Now create a class whose methods will be exposed to the world as web service. Use JBoss @BadgerFish annotation that supports to return response as JSON. To return JSON as response we need to use media type as application/json.
package com.concretepage.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
public class StudentService {
	public Student getStudentDetails(@PathParam("id") String id){
		Student student = new Student();
		student.setCollegeName("UP College");
		return student;

Maven Dependency of RESTful Web Service to Produce JSON

Find the maven dependency to support rest easy API.
To test the project, deploy the war file in tomcat or any other server and use the URL as below. http://localhost:8080/RestWB-1/restwb/10 where 10 is input to for web service. Response of the URL will be a JSON as below.

{"student":{"@id":"10","age":25,"collegeName":"UP College","name":"Ram"}}

Create Jersey Client to Consume JSON

If we want to consume web service response, then we need to use Jersey Client. Create WebResource instance using ClientConfig and provide media type as MediaType.APPLICATION_JSON because our client code is retrieving JSON response.
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-1/restwb/";
    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("10");
        String msg = msgService.accept(MediaType.APPLICATION_JSON).get(String.class);
Run as main method and find the output as below
{"student":{"@id":"10","age":25,"collegeName":"UP College","name":"Ram"}}

Maven dependency for Jersey Client

Find the maven dependency for Jersey Client.

Download Source Code


©2020 concretepage.com | Privacy Policy | Contact Us