December 16, 2011

EclipseLink MOXy is the JAXB Provider in WebLogic Server 12c

I am very happy to announce that EclipseLink JAXB (MOXy) is now the default JAXB (JSR-222) provider in WebLogic Server 12c.  I would like to thank the EclipseLink committers and the WebLogic team for all their hard work to make this happen.

In this post I will introduce how MOXy can be leveraged to create a JAX-WS service.  In future posts I will cover more of the extensions in greater detail.

WebLogic Server 12c can be downloaded from OTN at the following link:

December 5, 2011

Reusing Generated JAXB Classes

In this post I will demonstrate how to leverage the -episode XJC extension to reuse classes previously generated from.an XML schema.  This is useful when an XML schema is imported by other XML schemas and you do not want the same classes generated each time.

November 30, 2011

JAXB and Namespace Prefixes

In a previous post I covered how to use namespace qualification with JAXB.  In this post I will cover how to control the prefixes that are used.  This is not covered in the JAXB (JSR-222) specification but I will demonstrate the extensions available in both the reference and EclipseLink MOXy implementations for handling this use case.

October 12, 2011

JAXB (XJC) Imported Schemas and XML Catalogs

XML schema has a power mechanism called "import".  Import allows one XML schema to reference elements and types from another XML schema.  This means you could define types to represent commonly used information once and import these types into other XML schemas. Like any powerful tool, the import mechanism also has the ability to inflict pain.  In this post I'll demonstrate how to leverage an XML catalog to eliminate the pain when using JAXB's XJC tool to generate classes from an XML schema with imports.

September 30, 2011

Mixing Nesting and References with JAXB's XmlAdapter

Recently I came across a question on Stack Overflow asking if JAXB could marshal the first occurrence of an object as containment and all subsequent occurrences as a reference.  Below is an expanded version of my answer (up votes appreciated) demonstrating how this can be achieved by leveraging JAXB's XmlAdapter.

September 8, 2011

Mapping Objects to Multiple XML Schemas - Weather Example

I have written previous posts on EclipseLink JAXB (MOXy)'s @XmlPath and external binding file extensions.  In this post I will demonstrate how powerful these extensions are by mapping a single object model to two different XML schemas.  To make the example more "real", the XML data will come from two different services that provide weather information:  Google and Yahoo.

August 25, 2011

XML Schema to Java - Generating XmlAdapters

In previous posts I have demonstrated how powerful JAXB's XmlAdapter can be when starting from domain objects.  In this example I will demonstrate how to leverage an XmlAdapter when generating an object model from an XML schema.  This post was inspired by an answer I gave to a question on Stack Overflow (feel free to up vote).

August 24, 2011

JAXB and Enums

Today I answered a question on Stack Overflow (feel free to up vote) about JAXB and generating Java enums from an XML Schema.  This is normally straight forward, but there are a couple "gotchas" to be aware of.  In this post I'll demonstrate an easy and a harder use case.

August 23, 2011

JAXB and Java IO (Files, Streams, Readers, and Writers)

JAXB supports many different XML sources and targets.  In this post I'll give an overview of how JAXB can be used with those found in the java.io package: File, InputStream, OutputStream, Reader, Writer and their subclasses.

August 19, 2011

Binding to JSON & XML - Geocode Example

In a previous post I introduced the JSON binding that was added in the MOXy component in EclipseLink 2.4.  In that post I explained how the same metadata that is used for XML binding can be directly applied to JSON binding.  In this post I will demonstrate how powerful this is, by mapping one object model to both XML and JSON using the same set of metadata.  Google Maps Geocoding API V2 will be used to provide the XML and JSON inputs for this example.

You can try this out now using a download available from:

August 16, 2011

Using an Unmarshaller.Listener to Capture the Location

Recently I came across a question on Stack Overflow asking how find where in the document an object was unmarshalled from. In this post I'll expand on my answer of using an XMLStreamReader and Unmarshaller.Listener to accomplish this.

August 15, 2011

JSON Binding with EclipseLink MOXy - Twitter Example

In a previous post I described how the Jettison library could be used with any JAXB implementation (Metro, MOXy, JaxMe, etc) to produce/consume JSON.  Now I'll demonstrate the native object-to-JSON binding MOXy JAXB introduced in EclipseLink 2.4.  With MOXy as your JAXB provider you can produce/consume JSON using the standard JAXB APIs (available in Java SE 6) without adding any compile time dependencies.

You can try this out now using a download available from:

July 29, 2011

JAXB (XJC) and Nested Classes

Recently I came across a complaint on Twitter about how JAXB (XJC) generates deeply nested class structures from XML schemas.  In this post I want to first explain why JAXB does this, and second how to easily configure JAXB not to do this.

June 30, 2011

JAXB and Complex Types with Simple Content

A common question that appears on the forums is something like: "How do I map an object to an XML element with attributes and text in JAXB?".  The answer is to use JAXB's @XmlValue annotation.  In this post I'll demonstrate how this annotation can be leveraged.

Related Stack Overflow Questions:

June 27, 2011

JAXB and Factory Methods

Everyone knows that by default JAXB uses the no-argument constructor when instantiating objects.  Readers of this blog are also familiar with using XmlAdapters to build objects.  In this post I'll cover how the use of factory classes and methods can be configured through the @XmlType annotation.

June 24, 2011

Using JAXB's @XmlAccessorType to Configure Field or Property Access

JAXB offers a lot of flexibility when interacting with your object model.  One area is configuring the use of fields or properties to access the data in your domain objects.  This is specified as an XmlAccessType (PUBLIC_MEMBER, PROPERTY, FIELD, or NONE) via the @XmlAccessorType annotation.  In this post we'll examine what these options really mean.

June 22, 2011

EclipseLink 2.3 Release Available for Download

On behalf of the MOXy JAXB committers (great job by all), I am very proud to announce that EclipseLink 2.3 has been released and is available for download.  In this post I will summarize what is new in the MOXy component:

June 21, 2011

MOXy Extensible Models - Multiple Versions

Recently I have discussed support for extensible models in EclipseLink JAXB (MOXy) 2.3. The metadata for the extensions is supplied via MOXy's mapping document.  In this post I will demonstrate how to leverage MOXy's support for multiple mapping documents to keep your metadata organized.

June 14, 2011

MOXy Extensible Models - Refresh Example

In this example we will leverage EclipseLink JAXB (MOXy)'s concepts of externalized metadata (represented as a MetadataSource), and extensible models.  The MetadataSource will be used to define the metadata for the extensions.  In EclipseLink 2.3 we have introduced the ability for a JAXBContext to be "refreshed".  This means that without stopping the application we can supply metadata about new extensions.

June 3, 2011

Using EclipseLink MOXy with a Metadata Repository

EclipseLink JAXB (MOXy) provides an XML mapping document as an alternative to using annotations.  This mapping document can be provided in many different forms:  resource path, URL, File, Source, etc.  If however you store your metadata in some sort of repository this flexibility might not be enough.  This is why in EclipseLink 2.3 we have introduced a new mechanism to support this use case.

June 2, 2011

MOXy Extensible Models - Multi-Tenant Example

In a multi-tenant architecture a single application runs on a server, serving multiple client organizations (tenants).  Good multi-tenant applications allow per-tenant customizations.  When these customizations are made to data, it can be difficult for the binding layer to handle them.  In this post I'll demonstrate how to leverage EclipseLink JAXB (MOXy)'s support for extensible models to easily handle this use case.

June 1, 2011

Extensible Models with EclipseLink JAXB (MOXy)

JAXB is designed to work with domain models that have real fields/properties.  In EclipseLink 2.3, MOXy introduces the concept of virtual properties.  Virtual properties are defined by the MOXy metadata file, and provide a way to extend a class without modifying the source.  In this post I'll describe what virtual properties are, and in upcoming posts I'll cover how they can be leveraged:

Ignoring Inheritance with @XmlTransient

In previous articles I have covered how to map inheritance relationships in JAXB and EclipseLink MOXy.  In this example I will describe how to remove an inheritance relationship in JAXB by leveraging @XmlTransient at the type level.

May 30, 2011

JAXB and Joda-Time: Dates and Times

Joda-Time provides an alternative to the Date and Calendar classes currently provided in Java SE.  Since they are provided in a separate library JAXB does not provide a default mapping for these classes.  We can supply the necessary mapping via XmlAdapters.  In this post we will cover the following Joda-Time types:  DateTime, DateMidnight, LocalDate, LocalTime, LocalDateTime.

May 27, 2011

Overriding JAXB's "Name Mangling" Algorithm with EclipseLink MOXy

JAXB has well established rules for converting Java names to XML names.  If you want to override the default name you can always do so through the use of annotations.  Where this can become burdensome is if your names follow common rules (such as make everything upper case).  In the upcoming EclipseLink 2.3 release we have added a new MOXy JAXB feature that allows you to override the default naming algorithm.   You can try this out today using a nightly download:

May 24, 2011

JAXB and DTD - Apache log4j Example

There is a common misconception that JAXB requires an XML Schema.  This is understandable since JAXB offers the ability to derive Java classes from an XML schema, and generate XML schemas from annotated Java classes.  In reality the JAXB annotations are used to map XML to objects, and although not part of the JAXB specification, the XJC tool offers the ability to generate Java classes from a DTD.  In this post we will use JAXB to interact with XML that corresponds to the Apache log4j DTD.

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:

March 11, 2011

Preventing Entity Expansion Attacks in JAXB

This post will cover how to protect yourself from the entity expansion attack when you are using JAXB.

March 9, 2011

Handling Duplicate @XmlRootElement Declarations

The following post was inspired by an answer I gave to a question on Stack Overflow (feel free to up vote).  The problem arose from a JAXBContext being created on classes derived from two schemas that both use the same root element.  The solution is to supply type of object to the unmarshal operation.

March 8, 2011

Using JAXB to Implement a Copy Operation

The following post was inspired by an answer I gave to a question on Stack Overflow (feel free to up vote).  I am not suggesting that JAXB should be used to implement copy methods, instead I'm using a copy operation to demonstrate that JAXB can treat an object model as XML input.  

January 25, 2011

JAXB and Choosing the List Implementation

For elements with max occurs greater than one, JAXB will generate a java.util.List property and the underlying implementation will be java.util.ArrayList.  You can control which list implementation is used through internal and external schema annotations.  You can also use your own domain objects which gives you full control of your object model.  This post will discuss these different options.

January 20, 2011

JAXB and Date/Time Properties

In this post I will describe how JAXB handles date/time information.  This post will also cover how the @XmlSchemaType annotation can be used to customize the XML representation.