X

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

Guest Author


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:



href="https://github.com/arun-gupta/javaee7-samples/tree/master/jaxrs/moxy">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.

Join the discussion

Comments ( 8 )
  • Duc Luu Wednesday, July 24, 2013

    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?


  • Binh Thanh Nguyen Thursday, July 25, 2013

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


  • Arun Gupta Thursday, July 25, 2013

    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.


  • Hantsy Sunday, July 28, 2013

    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.


  • guest Thursday, August 1, 2013

    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?


  • Manu Friday, August 16, 2013

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


  • Arun Gupta Monday, September 2, 2013

    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


  • Blaise Doughan Tuesday, September 3, 2013

    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


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.