RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services and RESTful Java applications. Though this is not limited to be used in JBOSS only, and you can use with other servers also.
Jettison is a collection of Java APIs (like STaX and DOM) which read and write JSON. It is done using marshalling and and unmarshalling features.
In this post, I am demonstrating the way to use Jettison with RESTEasy, to convert the API response in json format.
Environment used:
- RESTEasy 2.3.1.GA
- RESTEasy Jettison provider 2.3.1
- Tomcat 7
- JDK 1.6
Follow below steps to build a demo application.
1) Create a maven project and convert to eclipse web project
mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false mvn eclipse:eclipse -Dwtpversion=2.0
2) Update runtime dependencies 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.howtodoinjava</groupId> <artifactId>RESTfulDemoApplication</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>RESTfulDemoApplication Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>jboss</id> <url>http://repository.jboss.org/maven2</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- core library --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.3.1.GA</version> </dependency> <dependency> <groupId>net.sf.scannotation</groupId> <artifactId>scannotation</artifactId> <version>1.0.2</version> </dependency> <!-- Jettison support --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jettison-provider</artifactId> <version>2.3.1.GA</version> </dependency> </dependencies> <build> <finalName>RESTfulDemoApplication</finalName> </build> </project>
3) Update web.xml file for resteasy specific servlet mapping
<!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> <!-- Auto scan REST service --> <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> <servlet> <servlet-name>resteasy-servlet</servlet-name> <servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher </servlet-class> </servlet> <servlet-mapping> <servlet-name>resteasy-servlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
4) Write a service class and model class
User.java
package com.howtodoinjava.model; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement(name = "user") public class User implements Serializable { private static final long serialVersionUID = 1L; @XmlAttribute(name = "id") private int id; @XmlElement(name = "firstName") private String firstName; @XmlElement(name = "lastName") private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
UserManagementModule.java
package com.howtodoinjava.service; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import com.howtodoinjava.model.User; @Path("/user-management") public class UserManagementModule { @GET @Path("/users/{id}") @Produces("application/json") public Response getUserById(@PathParam("id") Integer id) { User user = new User(); user.setId(id); user.setFirstName("Lokesh"); user.setLastName("Gupta"); return Response.status(200).entity(user).build(); } }
5) Run the application
When we deploy the above-built application in tomcat and hit the URL: "http://localhost:8080/RESTfulDemoApplication/user-management/users/10"
, below is the response.

Happy Learning !!
Matteo Toma
i fixed some errors..:
https://drive.google.com/drive/folders/1ZqcHBkarYSYl18shhGr_34Ko76kUQDBN?usp=drive_open
Lokesh Gupta
Thanks for sharing.
Debanjan Ghoshal
http status 500: Internal Server error running the same code. I am using tomcat 7 and I have downloaded the lib for jersey “jax-rs 2.22.1”. I am not using maven. Can you suggest what must be the error.
ramkumar
in my project i want to develop crud operation in restful java with jax-rs 2.0 and connect with database.give sample code
Lokesh Gupta
Could you try setting “MediaType.APPLICATION_JSON”?
Rakesh
i am getting following error :
Could not find MessageBodyWriter for response object of type: com.howtodoinjava.model.User of media type: application/json
Lokesh Gupta
Make sure you have following dependencies:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-mapper-asl.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
<scope>runtime</scope>
</dependency>
mankeomorakort
hi guy what tool do you use to test sending request?
Lokesh Gupta
https://addons.mozilla.org/en-US/firefox/addon/restclient/
narongmom
thank
Lokesh Gupta
Welcome
deepak
send me same example with jboss7