1

I have an issue with some RESTful services that takes a transfer object in parameter (basically an XML object that will be unmarshalled to a POJO).

@XmlRootElement(name = "myPojo")
public class MyPojo {
    @XmlElement(name = "myField")
    private Boolean myBoolean;

    public void setMyBoolean(Boolean bool) {
        myBoolean = bool;
    }

    public Boolean getMyBoolean()  {
        return myBoolean;
    }
}

And the service is something like that:

public class MyRestService {

    @PUT
    @Path("somewhere")
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response update(MyPojo pojo) {
        System.out.println("Boolean value: " + pojo.getMyBoolean();
    }
}

If I post this XML fragment:

<myPojo>
    <myField>false</myField>
</myPojo>

I got:

Boolean value: false

And if I post this XML fragment:

<myPojo>
    <myField>FALSE</myField>
</myPojo>

I got:

Boolean value: null

I run that code under Glassfish 4 with Jersey 1.9.1 and JAXB 2.2.7. In addition, under Glassfish 2, I got a different behavior where both uppercase and lowercase are unmarshalled as expected.

So, I am really curious to know what is happening and why the marshalling of boolean is different.

Thanks in advance

Laurent
  • 156
  • 2
  • 11
  • Try to use the primitive type rather than the wrapper type. You'll get, I assume, the value like [Boolean.valueOf(String)](http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html#valueOf(java.lang.String)) gives. – Jin Kwon Sep 11 '13 at 07:20

2 Answers2

3

I run into the same problem today where JAXB returns null when parsing "FaLsE" or "True" on a Boolean field. Unfortunately, upgrading to 2.2.7 or above (2.2.11 as of today) didn't help me. When I dig deeper into the source code, it seems that the parsing logic happens inside DatatypeConverterImpl.java. And there is no configuration that can alter its behavior on uppercase.

Link to JAXB DatatypeConverterImpl.java

The solution that I found (and works), is to define a new BooleanAdapter and asks JAXB to use that instead. In the adapter, you can define whatever conversion logic that fits your application.

Custom BooleanAdapter.java

public class BooleanAdapter extends XmlAdapter<String, Boolean> {

    @Override
    public Boolean unmarshal(String v) throws Exception {
        if (StringUtils.isBlank(v))
            return null;
        return Boolean.valueOf(v);
    }

    @Override
    public String marshal(Boolean v) throws Exception {
        if (v == null)
            return null;
        return v.toString();
    }
}

Your model object

@XmlElement(name = "myField")
@XmlJavaTypeAdapter(BooleanAdapter.class)
public Boolean getMyBoolean() {
    return myBoolean;
}
Lasgana
  • 31
  • 2
1

After several investigations, we figured out that the version 2.2 of JAXB we are using seems to contain a bug that serialize the boolean values not as expected. I mean that for example: FaLsE will be converted to null value.

Upgrading to the version 2.2.7 has fixed our issue.

Laurent
  • 156
  • 2
  • 11
  • Interesting. We're using 2.2 as well, and observed a case where an incoming XML request with a value "True" for a boolean field making it `null` after the JAXB binding occurs. – asgs May 13 '14 at 02:38