JSON-B test drive using JAX-RS

This is a quick post about JSON-B specification which is due in Java EE 8. It’s main purposes are

  • Provide JSON binding i.e. marshalling and un-marshalling to and from Java objects
  • Eclipselink will server as Reference Implementation
  • Supports Java object model to JSON mapping via annotations
  • Supports default mapping for implicit transformations b/w Java model and JSON (without annotation/additional metadata)
  • Produce JSON schema from Java classes
  • Support further customization of default mapping mechanisms as well as partial mapping (part of JSON doc needs to be mapped to Java model)
  • Will integrate with JSON-P and probably with JSON API JEP 198 (Java SE API for JSON processing) if needed

Please note that

  • this is just a quick test of the JSON-B implementation in a Java EE 7 container
  • not an ideal scenario i.e. it does not demonstrate integration of the JSON-B spec with a Java EE container (at least not yet)
  • I am not aware of Java EE 8 builds which ship with JSON-B implementation as of now

From a (Java EE 8) container integration perspective (near future)

the integration should/will be seamless and transparent – the way it is in case of JSON-P and JAXB annotated classes i.e.

  • you will ideally be able to annotate your Java classes using JSON-B annotations and
  • expect them to get serialized/deserialized automatically by the container (e.g. JAX-RS runtime) automatically

Overview of what was done

  • Have a simple POJO
  • Expose POJO using JAX-RS (GET, POST)
  • Use JAX-RS Message Body Reader and Writer – this overrides the default JSON-P providers and leverage JSON-B API here (simply use the default mapping feature) for transformation
  • Repeat the same for a ‘List of’ POJOs

Github project available here – this is not a Maven project. Ideally, you would want to clone into NetBeans, build and run on a Java EE 7 container. Make sure to have RI JAR in WAR (details below)

To be noted

  • I could not grab the Maven artifact of the JSON-B API JAR (maybe I am missing something), so ended up including the JSON-B API sources in the WAR itself
  • Same case with the RI – Downloaded it directly from here

More than this example, what’s important is the following

Cheers!

Advertisements

About Abhishek

Loves Java EE, distributed KV stores and messaging systems. Frequently blogs at abhirockzz.wordpress.com as well as simplydistributed.wordpress.com. Oh, I have also authored a few (mini) books, articles, Refcards etc. :-)
This entry was posted in Java, Java EE and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s