Over a million developers have joined DZone.

Using XmlAdapter for Testing

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

The XmlAdapter mechanism in JAXB (JSR-222) allows an unmappable class to be converted to a mappable one.  An XmlAdapter can be registered with the @XmlJavaTypeAdapter at the field, property, type, or package level.  This post will focus on the scope of the XmlAdapter when registered at the package level.

Package 1 - xmladapter.bar


When an XmlAdapter is registered at the package level it must include the type parameter.  The type parameter specifies the Java class that this XmlAdapter will be applied to.  For this package (xmladapter.bar) we will specify an XmlAdapter (StringAdapter) that will be applied to all fields/properties of type String within this package.

    @XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
package xmladapter.bar;

import javax.xml.bind.annotation.adapters.*; 


Our XmlAdapter will simply convert all instances of String to upper case when marshalling:

package xmladapter.bar;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class StringAdapter extends XmlAdapter<String, String> {

    public String unmarshal(String v) throws Exception {
        return v;

    public String marshal(String v) throws Exception {
        if(null == v) {
            return v;
        return v.toUpperCase();



Bar represents a POJO in this package with a property (name) of type String:

package xmladapter.bar;

public class Bar {

    private String name;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;


Package 2 - xmladapter.foo


Foo represents a domain object with a property of type String that exists in a different package (xmladapter.foo). The XmlAdapter we registered for the xmladapter.bar package will not apply to String fields/properties on this class:

package xmladapter.foo;

import javax.xml.bind.annotation.*;
import xmladapter.bar.Bar;

@XmlType(propOrder={"name", "bar"})
public class Foo {

    private String name;
    private Bar bar;

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

    public Bar getBar() {
        return bar;

    public void setBar(Bar bar) {
        this.bar = bar;



The following code will create instances of both Foo and Bar and marshal them to XML.  Note how the values set on the name properties for both Foo and Bar are lower case:

package xmladapter;

import javax.xml.bind.*;
import xmladapter.bar.Bar;
import xmladapter.foo.Foo;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class, Bar.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Foo foo = new Foo();

        Bar bar = new Bar();
        marshaller.marshal(foo, System.out);



Notice how the value of the name element within bar has been converted to upper case (line 5), but he name element within foo has not (line 3):

<?xml version="1.0" encoding="UTF-8"?>

Further Reading

If you enjoyed this post then you may also be interested in:

digg_url = 'http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html';

From http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}