JAXB


 

+

Search Tips   |   Advanced Search

 

JAXB transforms XML schemas to Java objects.

JAX-WS leverages JAXB as the binding technology for mappings between Java objects and XML documents.

WAS V7.0 supports the JAXB 2.1 specification, which provides enhancements such as improved compilation support and support for the @XMLSeeAlso annotation.

With JAXB 2.1, we can configure the xjc schema compiler so that it does not automatically generate new classes for a particular schema.

Configure the schemagen schema generator to not automatically generate a new schema, which is useful if we are using a common schema and do not want a new schema generated.

JAXB 2.1 introduces the @XMLSeeAlso annotation, which enables JAXB to know about all classes that are potentially involved in marshalling or unmarshalling as it is not always possible or practical to list all of the subclasses of a given Java class.

JAX-WS 2.1 also supports the use of the @XMLSeeAlso annotation on a service endpoint interface (SEI) or on a service implementation bean to ensure all of the classes referenced by the annotation are passed to JAXB for processing.

JAXB provides...

Use xjc schema to start with an XSD to create a set of Java Beans that map to the elements and types defined in the XSD schema.

We can also start with a set of Java Beans and use the schemagen schema generator tool to create the XML schema.

Once the mapping between XML schema and Java classes exists, XML instance documents can be converted to and from Java objects through the use of the JAXB binding runtime API. Data stored in XML documents can be accessed without the need to understand the data structure. We can then use the resulting Java classes to assemble a Web services application.

The JAXB runtime API supports marshaling of JAXB objects to XML and unmarshaling the XML document back to JAXB class instances.

We can use JAXB to provide XML validation to enforce both incoming and outgoing XML documents to conform to the XML constraints defined within the XML schema.

JAXB is the default data binding technology used by JAX-WS tooling.

We can use JAXB independently of JAX-WS.

 

JAXB collection types display as java.lang.List on JAX-WS operations

Technote (troubleshooting)

When you use JAXB customization to customize a collection property such as the following...

<xs:element name="createAccountResponse">
<xs:complexType >
    <xs:sequence>
        <xs:element name="return" type="ns10:items" maxOccurs="unbounded" minOccurs="0" >
            <xs:annotation>
                <xs:appinfo>
                    <jxb:property collectionType = "indexed"/>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
    </xs:sequence>
</xs:complexType>
</xs:element>

JAXB generates JavaBeans that satisfy the JAXB spec requirements for indexed properties. The JavaBeans contain getter and setter methods that return an internal implementation of an indexed array list.

The following example demonstrates what a JAXB generated JavaBean would look like when customized to an indexed array:

public Items[] getReturn()
public void setReturn(Items[] values)
public Items setReturn(int idx, Items value)
public Items getReturn(int idx)
public int getReturnLength()

However, the collection property within the JavaBean remains of the type...

java.lang.List.protectedList<Items>_return;

This problem causes a conflict when the type is referenced in a JAX-WS operation or when the type is referenced within another JAXB element. It is not possible to customize the JAX-WS types or the JAXB elements to contain anything else but a java.lang.List collection type. For example, when you start from WSDL, we are not able to generate the following:

public Items[] createAccount(

Instead, wsimport always generates a List, like in this example:

public List<Items> createAccount()

The problem is caused by the JAXB spec design for indexed properties.

 

Resolving the problem

As long as the schema and the WSDL are the same on both the client and server, the XML types can be mapped to different Java objects on each side.

It is therefore safe if the pre-existing JavaBeans or SEIs contain an indexed array type on the server through the use of wsgen, and a java.lang.List type on the client side through the use of wsimport.



Related concepts

JAX-WS

 

Related tasks

Use JAXB for XML data binding

 

Related

wsgen command for JAX-WS applications
wsimport command for JAX-WS applications
Web services specifications and APIs

 

Related information

JAXB 2.0 spec documentation