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 file in one of the packages for your domain classes, that is passed in to bootstrap the JAXBContext.