resteasy

JSON Example With RESTEasy + JAXB + Jettison

In this tutorial we are going to see how you can integrate RESTEasy with Jettison to develop JAX-RS RESTful services that produce and consume JSON streams. As you probably know, Jettison is a collection of APIs that are used to read and write JSON formatted streams. It can be coupled with XML serialization frameworks, like XStream, or JAXB used to marshal and unmarshal Java Objects to XML streams.

Jettison uses Badgerfish, which is a convention for translating an XML document into a JSON object. So, using Jettison alongside with JAXB, you can have your JAXB annotated Object mapped to JSON format. The one thing you have to do to enable the usage of Badgerfish in your REST Service is use the @BadgerFish annotation on the declaration of your service as you will see later on.

In this example we are not going to focus on how to create a JAX-RS application from top to bottom. So make sure you read carefully RESTEasy Hello World Example and pay attention to the sections concerning the creation of the project with Eclipse IDE as well as the deployment of the project in Tomcat.

You can create your own project following the instructions on RESTEasy Hello World Example. You should also take a look at the  JSON Example With RESTEasy + Jackson, download the code of this example : RESTEasyJSONExample.zip, and build your new project on top of that.

1. Project structure

For this example, I’ve created a new Project called “RESTEasyJSONExample“. You can see the structure of the NEW project in the image below:

project-structure

At this point you can also take a look at the web.xml file to see how the project is configured:

web.xml:

<?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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>JAXRS-RESTEasy</display-name>

	<servlet-mapping>
		<servlet-name>resteasy-servlet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>

	<!-- Auto scan REST service -->
	<context-param>
		<param-name>resteasy.scan</param-name>
		<param-value>true</param-value>
	</context-param>

	<!-- this should be the same URL pattern as the servlet-mapping property -->
	<context-param>
		<param-name>resteasy.servlet.mapping.prefix</param-name>
		<param-value>/rest</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
			</listener-class>
	</listener>

	<servlet>
		<servlet-name>resteasy-servlet</servlet-name>
		<servlet-class>
			org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
		</servlet-class>
	</servlet>

</web-app>

As you can see our servlet is mapped to /rest/ URI pattern. So the basic structure of the URIs to reach the REST Services used in this example will have the form :

http://localhost:8080/RESTEasyJSONExample/rest/...

2. Jettison and JAXB Dependencies

To integrate RESTEasy with Jettison and JAXB you have to declare the following dependencies in your pom.xml file.

Jettison and JAXB Dependencies:

<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>

3. Java class to be represented to JSON

This is the Java class that is going to be represented in JSON format.

Student.java:

package com.javacodegeeks.enterprise.rest.resteasy;

public class Student {

	private int id;
	private String firstName;
	private String lastName;
	private int age;

	// Must have no-argument constructor
	public Student() {

	}

	public Student(String fname, String lname, int age, int id) {
		this.firstName = fname;
		this.lastName = lname;
		this.age = age;
		this.id = id;
	}

	public void setFirstName(String fname) {
		this.firstName = fname;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setLastName(String lname) {
		this.lastName = lname;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return this.age;
	}

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

	public int getId() {
		return this.id;
	}

	@Override
	public String toString() {
		return new StringBuffer(" First Name : ").append(this.firstName)
				.append(" Last Name : ").append(this.lastName)
				.append(" Age : ").append(this.age).append(" ID : ")
				.append(this.id).toString();
	}

}

4. REST Service to produce JSON output

Let’s see how easy it is with RESTEasy to produce JSON output using a simple Student instance.

RESTEasyJSONServices.java:

package com.javacodegeeks.enterprise.rest.resteasy;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/jsonServices")
public class RESTEasyJSONServices {

	@BadgerFish
	@GET
	@Path("/print/{name}")
	@Produces("application/json")
	public Student produceJSON(@PathParam("name") String name) {

		Student st = new Student(name, "Marco", 19, 12);

		return st;

	}

}

Here, we use JAXB to map our Student instance to XML, and then use Badgerfish utility of Jettison to convert XML to JSON. This is one step too many, you might say, but notice that using this you can either choose to produce XML or JSON output. Additionally you can integrate Jettison with a number of other XML serializers like XStream.

After deploying the application, open your browser and go to:

http://localhost:8080/RESTEasyJSONExample/rest/jsonServices/print/James

This is the response:

browser

Here is the raw HTTP Response:

HTTP Response:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 09 Dec 2013 13:24:38 GMT

{"student":{"@id":"12","age":{"$":"19"},"firstName":{"$":"James"},"lastName":{"$":"Marco"}}}

5. REST Service to consume JSON

Here is a REST Service that consumes a simple JSON stream. the JSON object will be parsed and unmarshaled to Student instance.

RESTEasyJSONServices.java:

package com.javacodegeeks.enterprise.rest.resteasy;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/jsonServices")
public class RESTEasyJSONServices {

	@POST
	@Path("/send")
	@BadgerFish
	@Consumes("application/json")
	public Response consumeJSON(Student student) {

		String output = student.toString();

		return Response.status(200).entity(output).build();
	}

}

Now in order to consume that service we have to create a post request and append an XML file to it. For that we are going to use RESTEasy Client API. To use RESTEasy Client API you have to add the following dependency in your pom.xml.

RESTEasy Client API dependency:

<dependency>
	<groupId>org.jboss.resteasy</groupId>
	<artifactId>resteasy-client</artifactId>
	<version>3.0.4.Final</version>
</dependency>

For this, I’ve created a new class, called RESTEasyClient.java in a new Package called com.javacodegeeks.enterprise.rest.resteasy.resteasyclient. So the final Project Structure would be like so:

final-project-structure

Here is the client:

RESTEasyClient.java:

package com.javacodegeeks.enterprise.rest.resteasy.resteasyclient;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import com.javacodegeeks.enterprise.rest.resteasy.Student;

public class RESTEasyClient {

	public static void main(String[] args) {

		Student st = new Student("Captain", "H00k", 10, 12);

		/**
		 * Alternatively you can use this simple String to send instead of using
		 * a Student instance
		 * 
		 * String jsonString =
		 * "{\"student\":{\"@id\":\"12\",\"age\":10,\"firstName\":\"Captain\",\"lastName\":\"H00k\"}}"
		 * ;
		 */

		try {
			ResteasyClient client = new ResteasyClientBuilder().build();

			ResteasyWebTarget target = client
					.target("http://localhost:8080/RESTEasyJSONExample/rest/jsonServices/send");

			Response response = target.request().post(
					Entity.entity(st, "application/json"));

			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed : HTTP error code : "
						+ response.getStatus());
			}

			System.out.println("Server response : \n");
			System.out.println(response.readEntity(String.class));

			response.close();

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

}

As you can see, we create a simple Student instance and send it to the service via a POST Request. This is the output of the above client:

Outptut:

Server response : 

First Name : Captain Last Name : H00k Age : 10 ID : 1

Here is the raw POST request:

POST Request:

POST /RESTEasyJSONExample/rest/jsonServices/send HTTP/1.1
Content-Type: application/json
Accept-Encoding: gzip, deflate
Content-Length: 73
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.2.1 (java 1.5)

{"student":{"@id":"12","age":10,"firstName":"Captain","lastName":"H00k"}}

Note: Of course you can produce your POST request using any other tool that does the job. The example will work as long as you append the appropriate code in the POST Request body, like you see in the above request. For instance, you could simply write as a String in JSON format and append it to the request.

Download Eclipse Project

This was an JSON Example With RESTEasy + JAXB + Jettison. Download the Eclipse Project of this example: RESTEasyJSONExample.zip

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button