X

Consuming and Producing JSON using JAX-RS Entity Providers and JSR 353 Streaming API (TOTD# 210)

Guest Author


href="https://blogs.oracle.com/arungupta/entry/jax_rs_custom_entity_providers">TOTD
#193 explained how JAX-RS Entity Providers can be used to
provide mapping between on-the-wire representations and their
associated Java types. This blog shows how you can use Java API for
JSON Processing (JSR 353), already integrated in href="http://download.java.net/glassfish/4.0/promoted">GlassFish 4
promoted builds, to produce and consume JSON payload using
Entity Providers.



The source code in this blog can be href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/9c8898e11980cf71c98b1fa444dfe0e1/totd210_readerwriter_json.zip">downloaded
here and runs on href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b76.zip">GlassFish
b76.



Lets say your domain object is defined as:



public class MyObject {



  private String name;

  private int age;



  //. . .

}



And your resource endpoint is defined as:



@Path("endpoint")

public class MyResource {

  @POST

  @Consumes(MediaType.APPLICATION_JSON)

  public MyObject echoObject(MyObject mo) {

    return mo;

  }

}



This is just echoing the domain object but I suspect your
domain logic would be more complex than that ;-)



Using JAX-RS Client API, this endpoint can be invoked as:



WebTarget target = client.target(".../endpoint");

MyObject mo = target

              
.request()

              
.post(

                
Entity.entity(new MyObject("Duke", 18),
MediaType.APPLICATION_JSON),

                
MyObject.class);

System.out.println("Received response: " + mo.getName() + ", " +
mo.getAge() + "<br><br>");



The MessageBodyWriter.writeTo method, that writes MyObject
to the underlying OutputStream, uses Streaming API
from JSR 353 and looks like:



@Override

public void writeTo(MyObject t,

                   
Class<?> type,

                   
Type type1,

                   
Annotation[] antns,

                   
MediaType mt,

                   
MultivaluedMap<String, Object> mm,

                   
OutputStream out)

           
throws IOException, WebApplicationException {

  JsonGeneratorFactory factory =
Json.createGeneratorFactory();

  JsonGenerator gen = factory.createGenerator(out);

  gen.writeStartObject()

     .write("name", t.getName())

     .write("age", t.getAge())

     .writeEnd();

  gen.flush();

}



Similarly MessageBodyReader.readFrom method, that
reads MyObject from the underlying InputStream,
uses Streaming API from JSR 353 and looks like:



@Override

public MyObject readFrom(Class<MyObject> type,

                        
Type type1,

                        
Annotation[] antns,

                        
MediaType mt,

                        
MultivaluedMap<String, String> mm,

                        
InputStream in)

               
throws IOException, WebApplicationException {

  MyObject mo = new MyObject();

  JsonParser parser = Json.createParser(in);

  while (parser.hasNext()) {

    switch (parser.next()) {

      case KEY_NAME:

        String key =
parser.getString();

        parser.next();

        switch (key) {

          case "name":

           
mo.setName(parser.getString());

            break;

          case "age":

           
mo.setAge(parser.getIntValue());

            break;

          default:

            break;

        }

        break;

      default:

        break;

    }

  }

  return mo;

}



The code is pretty straight forward and refer to href="http://json-processing-spec.java.net/nonav/releases/1.0/pfd-draft/javadocs/index.html">Java
API for JSON Processing javadocs if you need help in
understanding the code.



Download the href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/9c8898e11980cf71c98b1fa444dfe0e1/totd210_readerwriter_json.zip">source
code and enjoy!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha