JAX-RS to Produce JSON using Jackson – Jersey 1.x

In this tutorial we are going to see how to produce JSON result type using JAX-RS RESTful web service (Jersey 1.x) and Jackson.

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;

public class Student {

	String name;
	int id;
	String department;
	int year;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

}

JSONService.java

package com.ehowtonow.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("producejson")
public class JSONService {

	@GET
	@Path("get")
	@Produces(MediaType.APPLICATION_JSON)
	public Student getStudentDetail() {
		Student student = new Student();
		student.setName("Ram");
		student.setId(33);
		student.setDepartment("EEE");
		student.setYear(2011);

		return student;
	}

}

In above program Jackson used to convert object to JSON. Jersey uses the @Produces(MediaType.APPLICATION_JSON) annotation will convert object to JSON automatically. For this 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: Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.ehowtonow.webservice.Student, and Java type class com.ehowtonow.webservice.Student, and MIME media type application/json was not found
	at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
	at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391)
	at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381)
	at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
	at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
	at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

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 service and access the service by calling the following URL

http://localhost:8080/Rest1xTutorials/rest/producejson/get

Output

{"name":"Ram","id":33,"department":"EEE","year":2011}

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. File Upload using JAX-RS RESTful Webservice (Jersey 2.x)
  2. Restful Webservice HelloWorld Example Jersey 1.x
  3. Download image using JAX-RS RESTful Webservice
  4. Download PDF file using JAX-RS RESTful Webservice
  5. JAX-RS @MatrixParam Restful Webservice
  6. JAX-RS @PathParm Web service Example
  7. JAX-RS @FormParam Web service example
  8. Download Excel file using JAX-RS RESTful Webservice
  9. JAX-RS get QueryParam using @Context UriInfo
  10. JAX-RS QueryParam – Jersey
  11. JAX-RS to Produce JSON using Jackson – Jersey 1.x
  12. JAX-RS Get HTTP Headers using @Context
  13. JAX-RS @POST using RESTful web service – Jersey 1.x
  14. JAX-RS Get HTTP Headers using @HeaderParam
  15. JAX-RS Web Service to produce XML using JAXB

Be the first to comment

Leave a Reply