Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

jaxb - Specify @XmlJavaTypeAdapter class via bindings file?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...