Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify @XmlJavaTypeAdapter class via bindings file?

Tags:

jaxb

xjc

I have a 3rd party interface that supplies xsd files that matches their API. Some of their mappings are not quite Java, the usual boolean as 0 & 1 :-(

I'd like to use a bindings file to specify the @XmlJavaTypeAdapter class for my BooleanAdapter, but so far no joy.

The bindings file:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="http://java.sun.com/xml/ns/jaxws"
    jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">
    <jaxb:bindings schemaLocation="GetUserDetailsResponse.xsd" node="/xs:schema" >
        <jaxb:globalBindings underscoreBinding="asWordSeparator" >
            <jaxb:serializable uid="1" />
            <jaxb:javaType name="java.lang.Boolean" xmlType="xs:boolean"
printMethod="mumble.bindings.BooleanAdapter.marshall" 
parseMethod="mumble.bindings.BooleanAdapter.unmarshall" />
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

And since I'm using maven the relevant bit from the POM:

<strict>false</strict>
<extension>true</extension>
<verbose>true</verbose>
<enableWrapperStyle>false</enableWrapperStyle>
<enableAsyncMapping>false</enableAsyncMapping>

I've toggled enableWrapperStyle and no change

What I end-up with is a generated Adapter of the wrong type:

import javax.xml.bind.annotation.adapters.XmlAdapter;
public class Adapter1
    extends XmlAdapter<String, Boolean>{
    public Boolean unmarshal(String value) {
        return (mumble.bindings.BooleanAdapter.unmarshall(value));
    }

    public String marshal(Boolean value) {
        return (mumble.bindings.BooleanAdapter.marshall(value));
    }
}

Is there some bindings file magic I can use to get rid of the generated wrapper and use the BooleanAdapter directly?

like image 660
Mike Summers Avatar asked Oct 20 '11 14:10

Mike Summers


People also ask

What is XJB file?

Generally we create a bindings file with . xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.

What is use of XSD file in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

Which command line command is used to generate XML schemas from Java source files?

You can create an XML schema document from an existing Java application that represents the data elements of a Java application by using the JAXB schema generator, schemagen command-line tool.


2 Answers

You need to use <xjc:javaType> in your binding config instead of <jaxb:javaType>. For example:

<xjc:javaType name="java.lang.Boolean" xmlType="xs:boolean"
              adapter="mumble.bindings.BooleanAdapter"/>

I understand I'm answering to an old question, but I don't have enough reputation to write a comment.

like image 64
mvv Avatar answered Oct 21 '22 20:10

mvv


It's a super late response, I realize, but even mvv's response left me struggling to totally grasp what I was doing and where in the structure the new element fit, so I wanted to add some further detail for anyone coming across this later.

Per mvv, the easiest answer is to change to using the xjc:javaType. See jaxb customization for the detailed documentation on using xjc:javaType.

You'll also need to change your custom adapter (BooleanAdapter) to implement the XmlAdapter interface.

Ultimately, your binding would instead look like this:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="http://java.sun.com/xml/ns/jaxws"
    jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">
    <jaxb:bindings schemaLocation="GetUserDetailsResponse.xsd" node="/xs:schema" >
        <jaxb:globalBindings underscoreBinding="asWordSeparator" >
            <jaxb:serializable uid="1" />
            <xjc:javaType name="java.lang.Boolean" xmlType="xs:boolean"
                  adapter="mumble.bindings.BooleanAdapter" />
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>
like image 23
jsparks Avatar answered Oct 21 '22 21:10

jsparks