Skip to content

Serializing a POJO to json using built in jersey support

thaggie edited this page Jul 11, 2014 · 5 revisions

Intro

On most jax-rs web services, you are going to have the need to take java objects and serialize to xml or json. This tutorial explains one method for taking a POJO and serializing to json utilizing built in support from jersey.

This tutorial assumes that you already have a jersey project. If not, follow the see the Create a "Hello World" jersey project tutorial.

Turn on POJO support

To turn on POJO support, add an init param to the servlet config in web.xml

<init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
</init-param>

My full web.xml now looks like:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>jersey sample</display-name>
	<servlet>
		<servlet-name>jersey</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>jayray</param-value>
		</init-param>
		
		<!-- the following is only needed if you want to use the built-in support
		for mapping pojo objects to json. -->
		<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</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

Specify dependencies

Add a dependency on the jersey-json library in your build.gradle file:

dependencies {
    compile "javax.ws.rs:jsr311-api:1.1.1"

    compile 'com.sun.jersey:jersey-server:1.13'
    compile 'com.sun.jersey:jersey-core:1.13'
    compile 'com.sun.jersey:jersey-servlet:1.13'
    compile 'com.sun.jersey:jersey-json:1.13'

    testCompile "junit:junit-dep:4.10"
}

Create POJO

Create a simple java DTO, such as:


public class Customer {
	private String id;
	private String name;
	private String city;
	private String state;

	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
}

Create a resource class

Create a resource class that will be the web service endpoint. On the @GET method, specify the media type as json using @Produces(MediaType.APPLICATION_JSON).

For example:

@Path("customer")
public class CustomerResource {
	@GET
	@Path("id/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Customer getCustomer(@PathParam("id") String id) {
		Customer customer = new Customer();
		customer.setId(id);
		customer.setCity("Austin");
		customer.setState("TX");
		customer.setName("Mighty Pulpo");
		return customer;
	}
}

Invoke endpoint

Compile, deploy, and exercise.

Using curl:

> curl -H Accept:application/json http://localhost:8080/jersey-starterkit/rest/customer/id/1
{"id":"1","name":"Mighty Pulpo","city":"austin","state":"TX"}

Further Reading