JSON Binding with JAX-RS - Moxy, JSON-P, Jackson, Jettison (TOTD #214)


One of the typical questions asked during presentations is how do I enable JSON support in JAX-RS ?

There are three basic approaches and four different modules in Jersey that enable them. Here is a quick summary:


MOXy
JSON-P
Jackson
Jettison
POJO-based JSON Binding
Yes
No
Yes
No
JAXB-based JSON Binding
Yes
No
Yes
Yes
Low-level JSON parsing & processing
No
Yes
No
Yes

MOXy is the default and preferred way of supporting JSON binding in your Jersey applications. A new sample is now added at:

https://github.com/arun-gupta/javaee7-samples/tree/master/jaxrs/moxy

The resource definition is:

@Path("endpoint")
public class MyResource {
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public MyObject echoObject(MyObject mo) {
        return mo;
    }
}


POJO is defined as:

public class MyObject {

    private String name;
    private int age;

    public MyObject() {
    }

    public MyObject(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

Application class is:

@ApplicationPath("webresources")
public class MyApplication extends Application {
}

And the client code is:

Client client = ClientBuilder.newClient();

WebTarget target = client.target("http://"
        + request.getServerName()
        + ":"
        + request.getServerPort()
        + request.getContextPath()
        + "/webresources/endpoint");
System.out.println("POST request");
MyObject mo = target
        .request()
        .post(Entity.entity(new MyObject("Duke", 18), MediaType.APPLICATION_JSON), MyObject.class);
out.println("Received response: " + mo.getName() + ", " + mo.getAge() + "<br><br>");

JSON MOXy module (jersey-media-moxy.jar) is in your classpath then Jersey automatically discovers the module and enable JSON binding support via MOXy.

jersey.java.net/documentation/latest/media.html provide more details on all the approaches and modules.
Comments:

Great, I was looking for more on JSON POJO mapping!

I used Jackson in combination with JAXB and @XmlRootElement / @XmlElement annotations to map from JSON attributes to POJO attributes. E.g. I wanted to map

{ "my_string": "something" }

to

@XmlElement(name = "my_string")
String myString;

Does Jersey also autmatically detect the right MOXy module for JAXB annotations?

Posted by Duc Luu on July 24, 2013 at 09:11 AM PDT #

I think Jersey, by default, uses Jackson to convert object to / form JSON not MOXy. Is it right?

Posted by Binh Thanh Nguyen on July 24, 2013 at 09:09 PM PDT #

Duc,

If you have MOXy dependency specified in your pom.xml then it auto-discover and register the feature.

Binh,

Jersey 2.0 now uses MOXy as a the default binding.

Posted by Arun Gupta on July 24, 2013 at 11:37 PM PDT #

Jackson has low level parsing and generating API, I have used it before, it includes 3 level api for JSON processing.

1. low level parsing and generating
2. JsonNode based APIs
3. Advanced direct Data binding.

Posted by Hantsy on July 27, 2013 at 08:39 PM PDT #

To use MOXy in combination with JAX-WS (example http://blog.bdoughan.com/2012/02/glassfish-312-is-full-of-moxy.html), I have to add the jaxws-eclipselink-plugin dependency on glassfish 4.0. On glassfish 3.1.2.2 this works out of the box because webservices-osgi.jar contains the com.sun.xml.ws.db.toplink package with the JAXBContextFactory for eclipselink.jaxb.

Is this a regression/known issue between glassfish 3.1.2.2 and 4.0?

Posted by guest on August 01, 2013 at 04:19 AM PDT #

Is there any standard JSON Schema Generation (or conversion from xsd), Schema based JSON Validation and Parsing tool/api available?

Posted by Manu on August 16, 2013 at 12:09 PM PDT #

Guest,

I've asked MOXy team to investigate the issue and they will get back.

Manu,

I suggest googling for "xml schema to json schema converter" for your answer, there are plenty of solutions. However there are several features in XML that are not available in JSON so an automated conversion may not work.

Arun

Posted by Arun Gupta on September 02, 2013 at 02:06 PM PDT #

Guest,

I have been able to reproduce the error you are seeing. You can use the following bug to track our progress on this issue:
- https://java.net/jira/browse/JAX_WS-1130

I am currently working with the JAX-WS team to see if there is a workaround you can use with the current release.

-Blaise

Posted by Blaise Doughan on September 03, 2013 at 09:18 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today