In this tutorial we are going to see how you can integrate RESTEasy withJackson to
develop JAX-RS RESTful services that produce and consume JSON streams. As you probably know, Jackson is used to marshal a Java Object to JSON, and ummarshal a JSON file (or stream in general) to a Java Object
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. But you can also download the Eclipse
project of this tutorial here : JAXRS-RESTEasy-CustomApplication.zip, and build your new code 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:
At this point you can also take a look at the web.xml
file
to see how the project is configured:
web.xml:
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
05 |
id = "WebApp_ID" version = "3.0" > |
06 |
< display-name >JAXRS-RESTEasy</ display-name > |
09 |
< servlet-name >resteasy-servlet</ servlet-name > |
10 |
< url-pattern >/rest/*</ url-pattern > |
15 |
< param-name >resteasy.scan</ param-name > |
16 |
< param-value >true</ param-value > |
21 |
< param-name >resteasy.servlet.mapping.prefix</ param-name > |
22 |
< param-value >/rest</ param-value > |
27 |
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap |
32 |
< servlet-name >resteasy-servlet</ servlet-name > |
34 |
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher |
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 :
2. Jackson Dependencies
To integrate RESTEasy with Jackson you have to declare the following dependencies in your pom.xml
file.
JSON/Jackson Dependencies:
2 |
< groupId >org.jboss.resteasy</ groupId > |
3 |
< artifactId >resteasy-jackson-provider</ artifactId > |
4 |
< version >3.0.4.Final</ version > |
3. Java class to be represented to JSON
This is the Java class that is going to be represented in JSON format.
Student.java:
01 |
package com.javacodegeeks.enterprise.rest.resteasy; |
03 |
public class Student
{ |
06 |
private String
firstName; |
07 |
private String
lastName; |
15 |
public Student(String
fname, String lname, int age, int id)
{ |
16 |
this .firstName
= fname; |
17 |
this .lastName
= lname; |
22 |
public void setFirstName(String
fname) { |
23 |
this .firstName
= fname; |
26 |
public String
getFirstName() { |
27 |
return this .firstName; |
30 |
public void setLastName(String
lname) { |
31 |
this .lastName
= lname; |
34 |
public String
getLastName() { |
38 |
public void setAge( int age)
{ |
46 |
public void setId( int id)
{ |
55 |
public String
toString() { |
56 |
return new StringBuffer( "
First Name : " ).append( this .firstName) |
57 |
.append( "
Last Name : " ).append( this .lastName) |
58 |
.append( "
Age : " ).append( this .age).append( "
ID : " ) |
59 |
.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:
01 |
package com.javacodegeeks.enterprise.rest.resteasy; |
03 |
import javax.ws.rs.Consumes; |
04 |
import javax.ws.rs.GET; |
05 |
import javax.ws.rs.POST; |
06 |
import javax.ws.rs.Path; |
07 |
import javax.ws.rs.PathParam; |
08 |
import javax.ws.rs.Produces; |
09 |
import javax.ws.rs.core.Response; |
11 |
@Path ( "/jsonServices" ) |
12 |
public class RESTEasyJSONServices
{ |
15 |
@Path ( "/print/{name}" ) |
16 |
@Produces ( "application/json" ) |
17 |
public Student
produceJSON( @PathParam ( "name" )
String name ) { |
19 |
Student
st = new Student(name, "Marco" , 19 , 12 ); |
After deploying the application, open your browser and go to:
This is the response:
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: Sun, 08 Dec 2013 16:45:50 GMT
{"id":12,"firstName":"James","lastName":"Marco","age":19}
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:
01 |
package com.javacodegeeks.enterprise.rest.resteasy; |
03 |
import javax.ws.rs.Consumes; |
04 |
import javax.ws.rs.GET; |
05 |
import javax.ws.rs.POST; |
06 |
import javax.ws.rs.Path; |
07 |
import javax.ws.rs.PathParam; |
08 |
import javax.ws.rs.Produces; |
09 |
import javax.ws.rs.core.Response; |
11 |
@Path ( "/jsonServices" ) |
12 |
public class RESTEasyJSONServices
{ |
16 |
@Consumes ( "application/json" ) |
17 |
public Response
consumeJSON( Student student ) { |
19 |
String
output = student.toString(); |
21 |
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:
2 |
< groupId >org.jboss.resteasy</ groupId > |
3 |
< artifactId >resteasy-client</ artifactId > |
4 |
< version >3.0.4.Final</ version > |
For this, I’ve created a new class, called RESTEasyClient.java
in
a new Package calledcom.javacodegeeks.enterprise.rest.resteasy.resteasyclient
.
So the final Project Structure would be like so:
Here is the client:
RESTEasyClient.java:
01 |
package com.javacodegeeks.enterprise.rest.resteasy.resteasyclient; |
03 |
import javax.ws.rs.client.Entity; |
04 |
import javax.ws.rs.core.Response; |
06 |
import org.jboss.resteasy.client.jaxrs.ResteasyClient; |
07 |
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder; |
08 |
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget; |
10 |
import com.javacodegeeks.enterprise.rest.resteasy.Student; |
12 |
public class RESTEasyClient
{ |
14 |
public static void main(String[]
args) { |
16 |
Student
st = new Student( "Catain" , "Hook" , 10 , 12 ); |
26 |
ResteasyClient
client = new ResteasyClientBuilder().build(); |
28 |
ResteasyWebTarget
target = client |
31 |
Response
response = target.request().post( |
32 |
Entity.entity(st, "application/json" )); |
34 |
if (response.getStatus()
!= 200 )
{ |
35 |
throw new RuntimeException( "Failed
: HTTP error code : " |
36 |
+
response.getStatus()); |
39 |
System.out.println( "Server
response : \n" ); |
40 |
System.out.println(response.readEntity(String. class )); |
44 |
} catch (Exception
e) { |
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 : Catain Last Name : Hook Age : 10 ID : 12
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: 57
Host: localhost:8080
Connection: Keep-Alive
{"id":12,"firstName":"Catain","lastName":"Hook","age":10}
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+ Jackson. Download the Eclipse Project of this example: RESTEasyJSONExample.zip