May 13, 2011

JAXB and Interface Fronted Models

Usually with JAXB we are mapping POJOs to XML.  In this example I will demonstrate how to handle a Java model that is exposed through a set of interfaces.   This post was inspired by an answer I gave (feel free to upvote) to a question on Stack Overflow.  In the question the poster was running into exceptions like the one below.  This post will cover how to properly map this use case.

Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 6 counts of IllegalAnnotationExceptions
blog.interfaces.Customer is an interface, and JAXB can't handle interfaces.
 this problem is related to the following location:
  at blog.interfaces.Customer
blog.interfaces.Customer does not have a no-arg default constructor.
 this problem is related to the following location:
  at blog.interfaces.Customer
...

May 5, 2011

Schema to Java: @XmlMimeType & @XmlInlineBinaryData

In a previous post I described the impact of the @XmlInlineBinaryData and @XmlMimeType annotations in a JAX-WS environment where binary data may be sent as attachments.  One of the comments I received on that post was how to have those annotations generated onto your classes when starting from an XML schema.  In this post I will address that question.

May 3, 2011

Specifying EclipseLink MOXy as Your JAXB Provider

To specify EclipseLink MOXy as the JAXB provider you need to put a jaxb.properties file in one of the packages for your domain classes, that is passed in to bootstrap the JAXBContext.

April 15, 2011

@XmlAnyElement and non-DOM Properties

This post will cover how to use an alternate DOM representation (i.e. String) with @XmlAnyElement.

JAXB and JSON via Jettison - Namespace Example

In a previous post I described how Jettison can be leveraged by a JAXB implementation to produce/consume JSON.  A reader correctly pointed out that I did not describe how to handle namespaces.  Since JSON does not support namespaces you would not normally include them in your mappings.  However if you wanted to map your object model to both JSON and XML with namespaces, this post will demonstrate how it can be done.

XML Schema to Java - XSD Choice

In a previous blog post I wrote about how to map to the choice structure in XML schema when starting from classes.  An astute reader tried generating an object model from that schema and noticed that the JAXB implementation generated something different than expected.  In this post I'll explain the difference.

April 12, 2011

JAXB and JSON via Jettison

JAXB implementations (Metro, EclipseLink MOXy, Apache JaxMe, etc) provide an easy means of converting objects to/from XML.  There is a library called Jettison that exposes access to JSON messages via the StAX API that a JAXB implementation can use to convert objects to/from JSON.  This library is being leveraged by a number of JAX-RS implementations.  In this post I'll demonstrate its use in a standalone example.

April 11, 2011

MOXy's XML Metadata in a JAX-RS Service

In previous posts I introduced how EclipseLink JAXB (MOXy) can represent it's metadata as XML, and how MOXy can be used in a JAX-RS service.  In this post I'll demonstrate how to leverage MOXy's metadata file in a JAX-RS service by using a ContextResolver.

March 23, 2011

JAXB, Web Services, and Binary Data

When an instance of a class is used with a Web Service, the JAX-WS implementation can choose to handle fields/properties that hold binary data as SOAP attachment.  An attachment is a means to send the data outside of the XML message, this is done as an optimization since binary data encoded as a xs:base64Binary string could be quite large.  JAXB offers a couple of annotations to control this behaviour:

  • @XmlInlineBinaryData
    This specifies that the binary data for this field/property must be written to the XML document as xs:base64Binary and not sent as an attachment.
  • @XmlMimeType
    For properties of type java.awt.Image or javax.xml.transform.Source, this annotation allows the mime type to be specified that will be used for encoding the data as bytes.

March 22, 2011

Map to Element based on an Attribute Value with EclipseLink JAXB (MOXy)

I am happy to announce that we are expanding MOXy's XPath based mapping in EclipseLink 2.3. You are now able to map to an element based on the value of an attribute.  This is done by leveraging XPath predicates via MOXy's @XmlPath annotation. 

You can try this out today by downloading one EclipseLink 2.3.0 nightly downloads starting on March 22, 2011 from: