【问题标题】:JAXB binding for JAX-WS SOAP request body parametersJAX-WS SOAP 请求主体参数的 JAXB 绑定
【发布时间】:2014-06-11 10:01:49
【问题描述】:

我正在用java 为Windows Phone 8(WP8) Enterprise MDM 编写一个SOAP Web 服务。客户端是WP8,它的SOAP请求会像下面这样,我无权更改这个请求的任何内容。

<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
   <s:Header>
      <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/windows/management/2012/01/enrollment/IDiscoveryService/Discover</a:Action>
      <a:MessageID>urn:uuid: 748132ec-a575-4329-b01b-6171a9cf8478</a:MessageID>
      <a:ReplyTo>
         <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
      </a:ReplyTo>
      <a:To s:mustUnderstand="1">https://ENROLLTEST.CONTOSO.COM/EnrollmentServer/Discovery.svc</a:To>
   </s:Header>
   <s:Body>
      <Discover xmlns="http://schemas.microsoft.com/windows/management/2012/01/enrollment/">
         <request xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <EmailAddress>user@contoso.com</EmailAddress>
            <RequestVersion>1.0</RequestVersion>
         </request>
      </Discover>
   </s:Body>
</s:Envelope>

以下是我的服务类,

@WebService(targetNamespace = "http://schemas.microsoft.com/windows/management/2012/01/enrollment/")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
@Addressing(enabled=true, required=true)
public interface DiscoveryService {

    @WebMethod(operationName = "Discover")
    SOAPMessage handleDiscoveryRequest(@WebParam(name = "request",targetNamespace = "http://www.w3.org/2001/XMLSchema-instance")
                                       DiscoveryRequest request) throws Exception;
}

以及请求体xml映射类,

@XmlRootElement(name = "request", namespace = "http://www.w3.org/2001/XMLSchema-instance")
@XmlAccessorType(XmlAccessType.FIELD)
public class DiscoveryRequest {

    @XmlElement(name = "EmailAddress", namespace = "http://www.w3.org/2001/XMLSchema-instance")
    private String emailId;

    @XmlElement(name = "RequestVersion", namespace = "http://www.w3.org/2001/XMLSchema-instance")
    private String version;

    // Getters and Setters
}

但是,从 SOAP-UI,我得到了以下带有生成 WSDL 的示例请求

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:enr="http://schemas.microsoft.com/windows/management/2012/01/enrollment/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Header/>
   <soap:Body>
      <enr:Discover>
         <!--Optional:-->
         <xsi:request>
            <!--Optional:-->
            <xsi:EmailAddress>?</xsi:EmailAddress>
            <!--Optional:-->
            <xsi:RequestVersion>?</xsi:RequestVersion>
         </xsi:request>
      </enr:Discover>
   </soap:Body>
</soap:Envelope>

而且,当我发送预期的请求(第一个代码快照)时,我在 SOAP-UI 中收到以下错误响应

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
   <soap:Header>
      <Action xmlns="http://www.w3.org/2005/08/addressing">http://schemas.microsoft.com/windows/management/2012/01/enrollment/DiscoveryService/Discover/Fault/UnmarshalException</Action>
      <MessageID xmlns="http://www.w3.org/2005/08/addressing">urn:uuid:d81dbc0f-cbd4-41dc-aa62-aed06f3c2dc6</MessageID>
      <To xmlns="http://www.w3.org/2005/08/addressing">http://www.w3.org/2005/08/addressing/anonymous</To>
      <RelatesTo xmlns="http://www.w3.org/2005/08/addressing">urn:uuid: 748132ec-a575-4329-b01b-6171a9cf8478</RelatesTo>
   </soap:Header>
   <soap:Body>
      <soap:Fault>
         <soap:Code>
            <soap:Value>soap:Sender</soap:Value>
         </soap:Code>
         <soap:Reason>
            <soap:Text xml:lang="en">Unmarshalling Error: unexpected element (uri:"http://schemas.microsoft.com/windows/management/2012/01/enrollment/", local:"request"). Expected elements are &lt;{http://www.w3.org/2001/XMLSchema-instance}request></soap:Text>
         </soap:Reason>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

有关如何解决此 JAXB 绑定命名空间问题的任何帮助?

Unmarshalling Error: unexpected element (uri:"http://schemas.microsoft.com/windows/management/2012/01/enrollment/", local:"request"). Expected elements are &lt;{http://www.w3.org/2001/XMLSchema-instance}request

【问题讨论】:

    标签: java web-services soap jaxb jax-ws


    【解决方案1】:

    示例请求和映射不匹配。

    在第一个请求中,元素“request”位于“.../enrollment”命名空间中。尽管为它定义了 xmlns:i,但没有设置前缀,因此它继承了在 parent 设置的默认命名空间:

    <Discover xmlns="http://schemas.microsoft.com/windows/management/2012/01/enrollment/">
         <request xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    

    所以错误信息是完全正确的。

    要使示例请求符合您的意图(并符合 WSDL),您应该有

         <i:request xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    

    注意'i:'前缀。

    如果请求是规范的(即,它是作为示例提供的正确请求),那么您显然应该更新您的映射,即将 XMLSchema-instance 作为 targetNamespace 删除。

    其余的看起来还不错。通常,在处理 SOAP 时,我建议也提供 WSDL。

    【讨论】:

    • 我无法更改请求。它由 WP8 定义。您能否详细说明删除目标名称空间?
    • 查看您的服务类和映射:在您看到 XMLSchema-instance 的任何地方,将其删除并替换为“.../enrollment”命名空间。元素“request”、“EmailAddress”和“RequestVersion”必须在“enrollment”命名空间中。
    • 好的,我做到了,现在我得到了,Action http://api.agent.windows.wso2.org/DiscoveryService/DiscoverRequest not supported 错误
    • 看到示例请求中的那些 mustUnderstand="1" 标头了吗?你必须在你的服务中支持他们。您需要实现 SOAPHandler 并使用这些标头。见这里:intertech.com/Blog/working-with-headers-in-jax-ws-soaphandlers
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多