April 12, 2013

Customizing EclipseLink JPA-RS Messages with MOXy

In a previous post I covered how EclipseLink JPA-RS can be used to expose a JPA persistence unit as a RESTful service.  In that example we interacted with the default message formats.  Since JPA-RS leverages MOXy for its XML and JSON binding we can use MOXy to customize the messages.  In this post I will demonstrate how this is done.

Introducing EclipseLink JPA-RS

In a previous series of posts I covered how to create a JAX-RS service that leveraged JPA for the persistence layer.  EclipseLink contains a component called JPA-RS that can be used to easily and automatically expose a persistence unit as RESTful service (that supports XML and JSON messages).  MOXy provides the XML and JSON-binding for JPA-RS and things like bidirectional mappings are automatically mapped for you.  In another post I cover how MOXy can be used to customize the messages shown in this example.

I will use the JPA model that I created in the posts below:

April 10, 2013

MOXy's Object Graphs - Handling Inheritance

In previous posts we have explored how object graphs can be defined through metadata and programatically.  In this post I'll demonstrate the impact of inheritance in your domain model on how you define object graphs.

You can try this out today by downloading an EclipseLink 2.5.0 nightly download starting on March 24, 2013 from:

April 8, 2013

Java API for JSON Processing (JSR-353) - Stream APIs

Java will soon have a standard set of APIs for processing JSON as part of Java EE 7.  This standard is being defined as JSR 353 - Java API for JSON Processing (JSON-P) and it is currently at the Final Approval Ballot.  JSON-P offers both object oriented and stream based approaches, in this post I will introduce the stream APIs.

You can get JSON-P reference implementation from the link below:
UPDATE

JSR-353 has passed the final approval ballot.

April 5, 2013

MOXy and JSON with Padding

JSON with padding is a communication mechanism used in JavaScript to overcome restrictions due to the same origin policy (for more information see: http://en.wikipedia.org/wiki/JSONP).  In this post I will demonstrate how to leverage JSON with padding when using MOXy as your JSON provider.

This functionality is new in EclipseLink 2.5.0 (and 2.4.2), you can try it out today using a nightly build available from:

April 3, 2013

MOXy's Object Graphs & Dynamic JAXB

JAXB (JSR-222) makes it easy for you to convert instances of your domain classes to/from XML.  The EclipseLink MOXy implementation offers an extension called Dynamic JAXB where instead of real classes you have instances of a map like class called DynamicEntity. You can access the data on your DynamicEntity using get and set methods that take the property name (i.e. customer.get("address") and customer.set('name", "Jane Doe").

In this post, first we will bootstrap a dynamic JAXBContext based on an external mapping file.  Then we unmarshal an XML document to dynamic entities, and finally we will apply an object graph to scope the resulting JSON output.

You can try this out today by downloading an EclipseLink 2.5.0 nightly download starting on March 24, 2013 from:

March 28, 2013

MOXy's Object Graphs - Partial Models on the Fly to/from XML & JSON

In a previous post I introduced how MOXy's Object Graph feature allows you to input and output partial models.  In that example I demonstrated how to define a partial model via metadata.  In this example we will create the object graphs programmatically.  This gives you additional flexibility when you need to create one on the fly.

You can try this out today by downloading an EclipseLink 2.5.0 nightly download starting on March 24, 2013 from:

March 26, 2013

MOXy's Object Graphs - Input/Output Partial Models to XML & JSON

Suppose you have a domain model that you want to expose as a RESTful service.  The problem is you only want to input/output part of your data.  Previously you would have created a separate model representing  the subset and then have code to move data between the models.  In EclipseLink 2.5.0 we have a new feature called Object Graphs that enables you to easily define partial views on your model.

You can try this out today by downloading an EclipseLink 2.5.0 nightly download starting on March 24, 2013 from:

March 15, 2013

Binding to JSON & XML - Handling Collections

One of EclipseLink JAXB (MOXy)'s strengths is the ability to map an object model to both JSON and XML with a single set of metadata.  The one weakness had been that you needed to compromise on the JSON key or XML element for collection properties.  I'm happy to say that this issue has been solved in EclipseLink 2.5 (and EclipseLink 2.4.2), and I will demonstrate below with an example.

You can try this out today by downloading an EclipseLink 2.5.0 (or EclipseLink 2.4.2) nightly build starting on March 15, 2013 from:

March 6, 2013

JAXB and java.util.Map

Is it ironic that it can be difficult to map the java.util.Map class in JAXB (JSR-222)?  In this post I will cover some items that will make it much easier.