Specify the @XmlJavaTypeAdapter class via the bindings file?

I have a third-party interface that supplies the xsd files corresponding to their API. Some of their mappings are not entirely Java, the usual boolean values ​​are 0 and 1: - (

I would like to use the bindings file to specify the @XmlJavaTypeAdapter class for my BooleanAdapter, but so far there has been no joy.

Binding 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 am using maven the corresponding bit from the POM:

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

I enabled enableWrapperStyle and did not change

What I end up with is the 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 any file magic binding that I can use to get rid of the generated wrapper and use the BooleanAdapter directly?

+7
source share
2 answers

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

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

I understand that I am answering an old question, but I do not have enough reputation to write a comment.

+10
source

This is a super late answer, I understand, but even the mvv answer left me trying to fully understand what I am doing, and where the new element is in the structure, so I wanted to add some details for those who come through this later.

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

You also need to modify your custom adapter (BooleanAdapter) to implement the XmlAdapter interface.

Ultimately, your binding will 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> 
+3
source

All Articles