简体   繁体   中英

JAXB Boolean with TRUE/FALSE in uppercase returning null

I have an issue with some Java first SOAP services implemented using Apache CXF 2.7.11 on Java 8. Value is turning out to be null when TRUE is passed as value. The same is working well when true is passed in lowercase.

I saw another thread where class extending XmlAdapter<String, Boolean> along with @XmlJavaTypeAdapter has resolved the issue, However the generated WSDL the type has been changed to xs:string from xs:boolean. Which means the boolean value should be passed as a string by consumer. Which is not acceptable in my case.

JAXB class:

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

    @XmlJavaTypeAdapter(CustomBooleanAdapter.class)
    public void setMyBoolean(Boolean bool) {
        myBoolean = bool;
    }

    public Boolean getMyBoolean()  {
        return myBoolean;
    }
}

Generated WSDL type

<xs:complexType name="myPojo">
    <xs:sequence>
        <xs:element minOccurs="0" name="myField" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

Now when I run the WSDL2Java on the service the generated class myField would be created as String rather than Boolean.

is there any way to keep the type intact in the generated WSDL?

Value is turning out to be null when TRUE is passed as value.

That is correct. TRUE is not a valid value for booleans in XML. See XML Schema Specification :

3.2.2.1 Lexical representation

An instance of a datatype that is defined as boolean can have the following legal literals { true, false, 1, 0 }.

If you use your CustomBooleanAdapter (I assume it looks a bit like this),

public static class CustomBooleanAdapter extends XmlAdapter<String, Boolean> {

  @Override
  public Boolean unmarshal(String s) throws Exception {
    return "TRUE".equals(s);
  }

  @Override
  public String marshal(Boolean b) throws Exception {
    if (b) {
      return "TRUE";
    }
    return "FALSE";
  }
}

you're defining a custom logic on top of the specification. The adapter converts a String to a boolean and that's the reason why your field is generated as string (it even says String in XmlAdapter<String, Boolean> ).

In principle you are asking for an XmlAdapter<Boolean,Boolean> which won't work with an upper case value "TRUE".

So either go with xs:string and TRUE or with xs:boolean and true. I really think you should use the proper boolean values from the specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM