JSON Response Using RESTful Web Service
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.xml
<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"> <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> <servlet> <servlet-name>resteasy-servlet</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>resteasy-servlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Create Class to Represent XML using JAXB
JSON data is converted from a bean. Bean is created using @XmlRootElement at class level.Student.java
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; @XmlAttribute public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getCollegeName() { return collegeName; } public void setCollegeName(String collegeName) { this.collegeName = collegeName; } public int getAge() { return age; } @XmlElement public void setAge(int age) { this.age = age; } }
@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.StudentService.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; import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish; @Path("/restwb") public class StudentService { @BadgerFish @GET @Path("/{id}") @Produces("application/json") public Student getStudentDetails(@PathParam("id") String id){ Student student = new Student(); student.setId(Integer.parseInt(id)); student.setName("Ram"); student.setCollegeName("UP College"); student.setAge(25); return student; } }
Maven Dependency of RESTful Web Service to Produce JSON
Find the maven dependency to support rest easy API.pom.xml
<dependencies> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.4.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>3.0.4.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jettison-provider</artifactId> <version>3.0.4.Final</version> </dependency> </dependencies>
{"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.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-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); System.out.println(msg); } }
{"student":{"@id":"10","age":25,"collegeName":"UP College","name":"Ram"}}
Maven dependency for Jersey Client
Find the maven dependency for Jersey Client.pom.xml
<dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.6</version> </dependency> </dependencies>