简体   繁体   English

具有大写TRUE / FALSE的JAXB布尔值,返回null

[英]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. 我在Java 8上使用Apache CXF 2.7.11实现的一些Java优先SOAP服务存在问题。当将TRUE作为值传递时,值变为null。 The same is working well when true is passed in lowercase. 当小写传递true时,同样有效。

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. 我看到另一个线程,其中扩展XmlAdapter<String, Boolean>@XmlJavaTypeAdapter一起解决了该问题,但是生成的WSDL类型已从xs:boolean更改为xs:string。 Which means the boolean value should be passed as a string by consumer. 这意味着布尔值应由使用者作为字符串传递。 Which is not acceptable in my case. 在我的情况下这是不可接受的。

JAXB class: JAXB类:

@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 生成的WSDL类型

<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. 现在,当我在该服务上运行WSDL2Java时,生成的类myField将创建为String而不是Boolean。

is there any way to keep the type intact in the generated WSDL? 有什么办法可以在生成的WSDL中保持类型不变?

Value is turning out to be null when TRUE is passed as value. 当将TRUE作为值传递时,值证明为空。

That is correct. 那是对的。 TRUE is not a valid value for booleans in XML. TRUE不是XML中布尔值的有效值。 See XML Schema Specification : 请参阅XML模式规范

3.2.2.1 Lexical representation 3.2.2.1词汇表示

An instance of a datatype that is defined as boolean can have the following legal literals { true, false, 1, 0 }. 定义为boolean的数据类型的实例可以具有以下合法文字{ true,false,1,0 }。

If you use your CustomBooleanAdapter (I assume it looks a bit like this), 如果您使用CustomBooleanAdapter(我认为它看起来像这样),

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> ). 适配器将String转换为布尔值,这就是为什么将字段生成为字符串的原因(它甚至在XmlAdapter<String, Boolean>表示String)。

In principle you are asking for an XmlAdapter<Boolean,Boolean> which won't work with an upper case value "TRUE". 原则上,您要使用的XmlAdapter<Boolean,Boolean>不能与大写值“ TRUE”一起使用。

So either go with xs:string and TRUE or with xs:boolean and true. 因此,要么使用xs:string和TRUE,要么使用xs:boolean和true。 I really think you should use the proper boolean values from the specification. 真的认为您应该使用规范中的正确布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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