JSON example with RESTEasy + JAXB + Jettison

RESTEasy uses Jettison JSON library to map JAXB annotation object to and from JSON. In this tutorial, we show you how to convert an JAXB annotated object into JSON format and return it back to client.

Jackson as JSON provider
You may also interest to read this RESTEasy + Jackson example.

1. RESTEasy + JAXB + Jettison

To use JSON in RESTEasy, you need following dependencies.

File : pom.xml


  <repositories>
	<repository>
		<id>JBoss repository</id>
		<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
	</repository>
  </repositories>

  <dependencies>

	<dependency>
		<groupId>org.jboss.resteasy</groupId>
		<artifactId>resteasy-jaxrs</artifactId>
		<version>2.2.1.GA</version>
	</dependency>

	<dependency>
		<groupId>org.jboss.resteasy</groupId>
		<artifactId>resteasy-jaxb-provider</artifactId>
		<version>2.2.0.GA</version>
	</dependency>
	
	<dependency>
		<groupId>org.jboss.resteasy</groupId>
		<artifactId>resteasy-jettison-provider</artifactId>
		<version>2.2.0.GA</version>
	</dependency>	

  </dependencies>

2. JAXB XML Provider

Create an object, annotate with JAXB. Why using XML provider? No worry, later you will use @BadgerFish to convert it into JSON format.


package com.mkyong.rest;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "movie")
public class Movie {

	String name;
	String director;
	int year;

	@XmlElement
	public String getName() {
		return name;
	}

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

	@XmlElement
	public String getDirector() {
		return director;
	}

	public void setDirector(String director) {
		this.director = director;
	}

	@XmlAttribute
	public int getYear() {
		return year;
	}

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

}

3. JAX-RS

To return a JSON file format, annotate the service method with @BadgerFish and @Produces("application/json").

RESTEasy will handle the JSON conversion automatically.


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;

@Path("/json/movie")
public class JSONService {

	@BadgerFish
	@GET
	@Path("/get")
	@Produces("application/json")
	public Movie getMovieInJSON() {

		Movie movie = new Movie();
		movie.setName("Transformers: Dark of the Moon");
		movie.setDirector("Michael Bay");
		movie.setYear(2011);
		
		return movie; 

	}

}

4. Demo

When URI pattern “/json/movie/get” is requested, following JSON file will be returned.


{
	"movie":
	{
		"@year":"2011",
		"director":{
			"$":"Michael Bay"
		},
		"name":{
			"$":"Transformers: Dark of the Moon"
		}
	}
}
result

Download Source Code

Download it – JAX-RS-Download-JSON-JAXB-Example.zip (7 KB)

References

  1. Jettison Official Website
  2. JAXB Official Website
  3. RESTEasy JAXB Provider
  4. Downlaod XML file from JAX-RS

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
8 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
ArunRoopGodwin ThayaleonardoNoel Recent comment authors
newest oldest most voted
Arun
Guest
Arun

How can i send JSON request to JAXB

Roop
Guest
Roop

How to remove $ and @ JASON keys from response

Godwin Thaya
Guest
Godwin Thaya

I have faces following issue in my jboss7.1.1Final and jboss 7.0.2 Give me suggestion to fix this issue

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/RESTfulDemoApplication]] (MSC service thread 1-6) Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap: java.lang.NoClassDefFoundError: javax/servlet/ServletContext

Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext from [Module “org.scannotation.scannotation:main” from local module loader @88b0b3 (roots: E:jboss-as-7.0.2.Finalmodules)]

leonardo
Guest
leonardo

your application throws an exception when I run with jboss 7.1.1

Exception:

Exception sending context initialized event to listener instance of class org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap: java.lang.RuntimeException: Unable to find a public constructor for class org.jboss.resteasy.core.AsynchronousDispatcher

You have solution for this?

Noel
Guest
Noel

Would like to see how you consume the JSON object.

Francois
Guest
Francois

Hi,

How do you filter attribute you want to produce ?
in your example, what can i do if i don’t want the Director attribute ?
and please, don t tell me to build dto !
thx

Francois
Guest
Francois

i precise : in json format

trackback
JAX-RS Tutorial

[…] from JSON.JSON example with RESTEasy + Jackson RESTEasy + Jackson to map object to and from JSON.JSON example with RESTEasy + JAXB + Jettison RESTEasy + JAXB + Jettison to map object to and from JSON.RESTful Java clientsCreate a RESTful Java […]

trackback
Integrate Jackson with RESTEasy

[…] and return it back to the client. Jettison as JSON provider You may also interest to read this RESTEasy + Jettison + JAXB example.1. RESTEasy + JacksonTo integrate Jackson with RESTEasy, you just need to include […]