Difference between revisions of "EclipseLink/UserGuide/MOXy/Mapping the Unmappable/Converters"

From Eclipsepedia

Jump to: navigation, search
(New page: '''Example''' *http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html)
 
Line 1: Line 1:
'''Example'''
+
{{EclipseLink_UserGuide
*http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
+
|info=y
 +
|toc=y
 +
|eclipselink=y
 +
|eclipselinktype=MOXy
 +
|api=y
 +
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/adapters/XmlAdapter.html XmlAdapter]
 +
* [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html XmlJavaTypeAdapter]
 +
}}
 +
 
 +
= XmlAdapter =
 +
 
 +
JAXB's '''XmlAdapter''' can be used to map classes that would normally be considered "unmappable" - such as classes that do not have a default no-arg constructor, or classes that do not have a natural XML representation.   In a custom subclass of '''XmlAdapter''', you can define define custom code to convert the unmappable class into something that JAXB can handle.  Then, you can use the '''XmlJavaTypeAdapter''' annotation to indicate that your adapter should be used.
 +
 
 +
= Example =
 +
 
 +
Our first example will use the following domain class:
 +
 
 +
<source lang="java">
 +
package example;
 +
 
 +
import java.util.Currency;
 +
 
 +
@XmlRootElement
 +
@XmlAccessorType(XmlAccessType.FIELD)
 +
public class PurchaseOrder {
 +
 
 +
  private Double amount;
 +
 
 +
  private Currency currency;
 +
 
 +
  ...
 +
}
 +
</source>

Revision as of 15:48, 6 June 2011

EclipseLink MOXy

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Contents


XmlAdapter

JAXB's XmlAdapter can be used to map classes that would normally be considered "unmappable" - such as classes that do not have a default no-arg constructor, or classes that do not have a natural XML representation. In a custom subclass of XmlAdapter, you can define define custom code to convert the unmappable class into something that JAXB can handle. Then, you can use the XmlJavaTypeAdapter annotation to indicate that your adapter should be used.

Example

Our first example will use the following domain class:

package example;
 
import java.util.Currency;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PurchaseOrder {
 
   private Double amount;
 
   private Currency currency;
 
   ...
}