【问题标题】:Why does wsimport add @XmlSchemaType(name = "string")?为什么 wsimport 添加@XmlSchemaType(name = "string")?
【发布时间】:2015-02-26 19:40:50
【问题描述】:

当我使用更新版本的 wsimport 时会发生这种情况。下面的示例 WSDL 包含一个 SimpleType“SomeEnum”,它成为 JAXB 类中的一个枚举。我有一些代码可以从 JAXB 类生成 XSD 模式,以前它会正确地将类型 tns:SomeEnum 分配给该元素,但现在将其视为字符串。

我最终发现 wsimport 正在添加注解 @XmlSchemaType(name = "string"),导致了这种行为。这对我来说似乎是错误的,因为有比字符串更具体的类型。它还破坏了依赖于反向生成模式的代码。

这是我能想出的用于重现错误的最小 WSDL。

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
  targetNamespace="http://foo.bar.com/example"
  xmlns:tns="http://foo.bar.com/example"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <wsdl:types>
    <schema elementFormDefault="qualified" jaxb:version="1.0"
      targetNamespace="http://foo.bar.com/example"
      xmlns="http://www.w3.org/2001/XMLSchema"
      xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:tns="http://foo.bar.com/example">
      <annotation>
        <appinfo>
          <jaxb:globalBindings typesafeEnumMaxMembers="999999"/>
        </appinfo>
      </annotation>
      <complexType name="Wrapper">
        <sequence>
          <element maxOccurs="1" minOccurs="0" name="someEnum" type="tns:SomeEnum"/>
        </sequence>
      </complexType>
      <simpleType name="SomeEnum">
        <restriction base="xsd:string">
          <enumeration value="SOME_VALUE"/>
          <enumeration value="ANOTHER_VALUE"/>
        </restriction>
      </simpleType>
      <element name="doesNothing">
        <complexType>
          <sequence>
          </sequence>
        </complexType>
      </element>
    </schema>
  </wsdl:types>

  <wsdl:message name="doesNothingRequest">
    <wsdl:part element="tns:doesNothing" name="parameters"/>
  </wsdl:message>

  <wsdl:portType name="SimpleTypeIssueServiceInterface">
    <wsdl:operation name="doesNothing">
      <wsdl:input message="tns:doesNothingRequest" name="doesNothingRequest"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="SimpleTypeIssueServiceSoapBinding" type="tns:SimpleTypeIssueServiceInterface">
    <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="doesNothing">
      <wsdlsoap:operation soapAction=""/>
      <wsdl:input name="doesNothingRequest">
        <wsdlsoap:body use="literal"/>
      </wsdl:input>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SimpleTypeIssueService">
    <wsdl:port binding="tns:SimpleTypeIssueServiceSoapBinding" name="SimpleTypeIssueServiceInterfacePort">
      <wsdlsoap:address location="LOCATION_TEMPLATE"/>
    </wsdl:port>
  </wsdl:service>

这里是生成的 Wrapper.java 的片段。 Java7 wsimport(我想要)中缺少@XmlSchemaType 批注,并且出现在Java8 wsimport 中。我不知道这是修复错误还是引入错误。

...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Wrapper", propOrder = {
    "someEnum"
})
public class Wrapper {

    @XmlSchemaType(name = "string") // [Why is this added?]
    protected SomeEnum someEnum;

    /**
     * Gets the value of the someEnum property.
     *
     * @return
     *     possible object is
     *     {@link SomeEnum }
     *
     */
    public SomeEnum getSomeEnum() {
        return someEnum;
    }
...
}

【问题讨论】:

标签: java jaxb wsdl wsimport


【解决方案1】:

@XmlSchemaType(name = "string") protected SomeEnum someEnum; 这意味着这个 java 属性的 xsd 类型是 xsd:string (base="xsd:string")。详细解释看A JAXB Nuance: String Versus Enum from Enumerated Restricted XSD String这个。

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多