RESTful Web service to consume JSON and Produce XML – Jersey 1.x

In this tutorial we are going to see about how to consume JSON and Produce XML using JAX-RS RESTful web service (Jersey 1.x)

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 1.x maven dependency configuration below. If you are new to maven refer our maven tutorials.

Jersey dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
     <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
    	<groupId>com.sun.jersey</groupId>
    	<artifactId>jersey-json</artifactId>
    	<version>1.17.1</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

Student.java

package com.ehowtonow.webservice;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student")
public class Student {

 
 String name;
 int id;
 String department;
 int year;
 
 @XmlElement
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @XmlAttribute
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }

 @XmlElement
 public String getDepartment() {
  return department;
 }
 public void setDepartment(String department) {
  this.department = department;
 }
 
 @XmlElement
 public int getYear() {
  return year;
 }
 public void setYear(int year) {
  this.year = year;
 }
 
}

In above class annotations used for JAXB (to produce XML) and same class used to consume JSON

RestConsumeProduce.java

package com.ehowtonow.webservice;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("restconsumeproduce")
public class RestConsumeProduce {

 @POST
 @Path("post")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_XML)
 public Student getStudentDetail(Student student) {
  return student;
 }
}

In above program @Consumes(MediaType.APPLICATION_JSON) accept JSON data as input and convert the JSON data to Java object and data automatically, For this JSON to Java object conversion jersey-json.jar is required and init-parm com.sun.jersey.api.json.POJOMappingFeature should be set to true in web.xml (Jersey to Support JSON-Object mapping) , otherwise you will get the below error.

SEVERE: A message body reader for Java class com.ehowtonow.webservice.Student, and Java type class com.ehowtonow.webservice.Student, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
application/json ->
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

Configure Jersey Servlet dispatcher

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</display-name>
	<servlet>
		<servlet-name>jersey-serlvet</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.ehowtonow.webservice</param-value>
		</init-param>
		<init-param>
			<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
			<param-value>true</param-value>
		</init-param>

		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

Now you can run the client and see the following output.

output for Consume JSON And Produce XML Service

output <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?><student id=”33″><department>EEE</department><name>Ram</name><year>2011</year></student>


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. JAX-RS Get HTTP Headers using @Context
  2. JAX-RS @QueryParam with @DefaultValue – Jersey
  3. Download image using JAX-RS RESTful Webservice
  4. JAX-RS Get HTTP Headers using @HeaderParam
  5. JAX-RS to Produce JSON using Jackson – Jersey 1.x
  6. JAX-RS Send List using QueryParam Annotation
  7. JAX-RS QueryParam – Jersey
  8. Download Text file using JAX-RS RESTful Webservice
  9. JAX-RS get QueryParam using @Context UriInfo
  10. JAX-RS @FormParam Web service example
  11. Download PDF file using JAX-RS RESTful Webservice
  12. Download Excel file using JAX-RS RESTful Webservice
  13. JAX-RS @POST using RESTful web service – Jersey 1.x
  14. Restful Webservice HelloWorld Jersey 2.x Example
  15. JAX-RS @MatrixParam Restful Webservice

Be the first to comment

Leave a Reply