Lucas Oliveira
Lucas Oliveira

Reputation: 3477

Jaxb2Marshaller: use 0/1 to marshall xsd:boolean values with generated java model

I am using Jaxb2Marshaller's to marshal and unmarshal soap requests and responses.

My java model classes are generated based on a wsdl using xjc.

I have a strange requirement that fields of type xsd:boolean should be marshaled as 1 or 0 in the result xml( by default "true" or "false" values are used).

I already tried to create a XmlAdapter as follows:

public class JaxbBooleanAdapter extends XmlAdapter<Integer, Boolean> {

    @Override
    public Boolean unmarshal(final Integer v)
    {
        return v == null ? null : v == 1;
    }

    @Override
    public Integer marshal(final Boolean v)
    {
        return v == null ? null : v ? 1 : 0;
    }

}

and setted my Jaxb2Marshaller to use it:

marshaller.setAdapters( new JaxbBooleanAdapter() );

but it did not work.

When i annotated the java model class with @XmlJavaTypeAdapter( JaxbBooleanAdapter.class ) it worked perfecly, but my java model is generated by xjc and i could not make it generate the annotations using bindings, i tried the following binding:

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xsd="http://w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">

    <globalBindings>
        <xjc:javaType xmlType="xsd:boolean" name="java.lang.Boolean"
            adapter="br.com.neolog.label.integration.configuration.JaxbBooleanAdapter" />
    </globalBindings>
</bindings>

My question is: Is there any way to force a Jaxb2Marshaller to marshal boolean values as 0/1 for java model classes generated with xjc?

Upvotes: 2

Views: 567

Answers (0)

Related Questions