4

关于问题

我使用 Wsdl2Java 从 wsdl 文件生成 java 类。使用生成的 Web 服务接口时,Jaxb 会抛出异常“unknown unmarshaller”。该问题存在于其输入参数是 xs:dateTime 类型的元素的操作中,例如此模式:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="asd:asd"
  elementFormDefault="qualified">
  <xsd:element name="DateSinceStart" type="xsd:dateTime"/>
</xsd:schema>

并且 JAXB 具有绑定:

<globalBindings>
    <javaType
            name="java.time.LocalDateTime"
            xmlType="xs:dateTime"
            parseMethod="LocalDateTimeAdapter.unmarshal"
            printMethod="LocalDateTimeAdapter.marshal"/>
</globalBindings>

Jaxws 绑定:

<bindings xmlns="http://java.sun.com/xml/ns/jaxws">
    <!-- Disable default wrapper style -->
    <enableWrapperStyle>false</enableWrapperStyle>
</bindings>

然后,生成的 MyServiceInterface.java 将 LocalDateTime 作为输入参数。JaxB 从 unmarshall 方法抛出异常,因为 unmarshaller 对上下文是未知的。

我对生成器的期望

在 MyServiceInterface.java 文件中,生成器应@XmlJavaTypeAdapter(Adapter1.class)在 LocalDateTime 输入参数上方添加一行。手动插入工作并使 JAXB 能够理解如何解组这种类型:

@WebService(targetNamespace = "asd:asd", name = "MyServiceInterface")
@XmlSeeAlso({asd.asd.ObjectFactory.class})
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface MyServiceInterface {

    @WebMethod(operationName = "Operation", action = "asd:asd:#Operation")
    @WebResult(name = "OperationResponse", targetNamespace = "asd:asd", partName = "OperationResponseBody")
    public asd.asd.OperationResponseType operation(
        @WebParam(partName = "OperationBody", name = "DateSinceStart", targetNamespace = "asd:asd")
        @XmlJavaTypeAdapter(Adapter1.class)
        java.time.LocalDateTime operationBody
    );
}

问题

为什么生成器不能自动插入注释?它是框架中的错误吗?

注意:如果参数被请求类包裹,@XmlJavaAdapter则正确放置在相应字段的上方。

语境

下面我粘贴合同文件只是为了了解我的问题:

我的服务.wsdl:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  xmlns:tt="asd:asd"
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  name="MyService"
  targetNamespace="asd:asd">
  <wsdl:documentation>
    <ServiceName>MyService</ServiceName>
    <Version>1</Version>
  </wsdl:documentation>
  <wsdl:types>
    <xsd:schema elementFormDefault="qualified" targetNamespace="asd:asd">
      <xsd:include schemaLocation="Operation.xsd"/>
      <xsd:include schemaLocation="OperationResponse.xsd"/>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="OperationMessage">
    <wsdl:part element="tt:DateSinceStart" name="OperationBody"/>
  </wsdl:message>
  <wsdl:message name="OperationResponseMessage">
    <wsdl:part element="tt:Response" name="OperationResponseBody"/>
  </wsdl:message>
  <wsdl:portType name="MyServiceInterface">
    <wsdl:operation name="Operation">
      <wsdl:input message="tt:OperationMessage" name="OperationIn">
      </wsdl:input>
      <wsdl:output message="tt:OperationResponseMessage" name="OperationOut">
      </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="MyServiceBinding" type="tt:MyServiceInterface">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="Operation">
      <soap:operation soapAction="asd:asd:#Operation"/>
      <wsdl:input name="OperationIn">
        <soap:body parts="OperationBody" use="literal"/>
      </wsdl:input>
      <wsdl:output name="OperationOut">
        <soap:body parts="OperationResponseBody" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding> 
  <wsdl:service name="MyService">
    <wsdl:port binding="tt:MyServiceBinding" name="MyService">
      <soap:address location="http://address"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

操作.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="asd:asd"
  elementFormDefault="qualified">
  <xsd:element name="DateSinceStart" type="xsd:dateTime"/>
</xsd:schema>

我不粘贴 OperationResponse.xsd 内容,因为它包含 xs:complexType 的元素,并且所有日期@XmlJavaAdapter时间在相应的 LocalDateTime 字段处都有注释。复杂类型就可以了。

4

0 回答 0