3
votes

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?

1
If the XML element is upposed to contain a boolean value ... Why not simply declare it as one (type="xs:boolean")? Or did I misunderstand your question?Seelenvirtuose
@Seelenvirtuose The OP seems to generate schema from the model and gets type="xs:string". The whole question is about how to get type="xs:boolean" generated.lexicore
@lexicore OP speaks about wsdl2java which generates the model from a WSDL. Puzzled? Edit: OK. Last sentence speaks about a generated WSDL. Hmmm ...Seelenvirtuose
@Seelenvirtuose Seems like it's actually first Java -> WSDL to get the WSDL. Which is generated with the wrong type. The OP worries about it as later on, when (probably someone else) does WSDL -> Java there will be a wrong type in Java.lexicore
@lexicore - Yes that is the problem. It is a Java first SOAP service and the requirement is to support case insensitive boolean values. Applying XmlAdapter has impact on the stub / client classes generated from WSDL.Ganesh S

1 Answers

7
votes

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.