【问题标题】:SOAP service response cannot be mapped无法映射 SOAP 服务响应
【发布时间】:2015-05-06 14:14:40
【问题描述】:

这个问题困扰了我将近两天,我真的需要一些帮助来解决这个问题。

我使用 wsimport 为一个 Java 项目从两个不同的 .wsdl 文件生成代码。

第一个服务工作得很好,但由于某种原因,第二个服务的响应无法解组到响应对象。

工作服务:

@WebMethod(action = "[actionName]")
@WebResult(name = "getSimpleCompanyInfoResponse", partName = "getSimpleCompanyInfoResponse")
public GetSimpleCompanyInfoResponse getSimpleCompanyInfo(
        @WebParam(name = "getSimpleCompanyInfoRequest", partName = "getSimpleCompanyInfoRequest") GetSimpleCompanyInfoRequest getSimpleCompanyInfoRequest);

响应 POJO:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getSimpleCompanyInfoResponse", propOrder = {
    //variables
})
public class GetSimpleCompanyInfoResponse {
    //variables
}

响应 XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="[namespaceUri]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
        <ns1:getSimpleCompanyInfoResponse>
            <getSimpleCompanyInfoResponse>
                //variables
            </getSimpleCompanyInfoResponse>
        </ns1:getSimpleCompanyInfoResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

工作服务:

@WebMethod(operationName = "PersonnelInfo", action = "[actionName]")
@WebResult(name = "PersonnelInfoResponse", partName = "PersonnelInfoResponse")
public PersonnelInfoResponse personnelInfo(
@WebParam(name = "PersonnelInfoRequest", partName = "PersonnelInfoRequest") PersonnelInfoRequest personnelInfoRequest);

响应 POJO:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonnelInfoResponse", propOrder = {
    //variables
})
public class PersonnelInfoResponse {
    //variables
}

响应 XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="[namespaceUri]" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
        <ns1:PersonnelInfoResponse>
            //variables
        </ns1:PersonnelInfoResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用 -Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=true 或使用 Wireshark 进行监控,我可以看到来自第二个服务的响应信封刚刚通过很好,解组不会引发任何异常,但最终 PersonnelInfoResponse 为空。

我能看到的唯一区别是 XML 中的第二个服务响应负载缺少外部元素,这似乎是问题所在。但是,我不知道如何“修复”它,因此它不会寻找外部元素。

如果有任何不清楚或遗漏的地方,请告诉我,我会尽力为您提供所有信息。

编辑:

不,很遗憾,我无法控制服务本身,我只有 .wsdl 和 .xsd。

我这样调用服务:

ReportsControllerPortType port = new ReportsControllerService().getReportsControllerPort();

PersonnelInfoRequest request = new PersonnelInfoRequest();
//fill the required fields in the request, username, password, etc.

PersonnelInfoResponse response = port.personnelInfo(request);

客户端服务存根(ReportsControllerService & ReportsControllerPortType)也是由wsimport根据.wsdl和.xsd自动生成的。

编辑 2:

删除操作名无效,服务初始化失败。以下是两个 .wsdl-s 中的定义:

工作服务:

<wsdl:message name="getSimpleCompanyInfoRequest">
    <wsdl:part name="getSimpleCompanyInfoRequest" type="tns:getSimpleCompanyInfoRequest" />
</wsdl:message>
<wsdl:message name="getSimpleCompanyInfoResponse">
    <wsdl:part name="getSimpleCompanyInfoResponse" type="tns:getSimpleCompanyInfoResponse" />
</wsdl:message>

<wsdl:portType name="MonitoringControllerPortType">
    <wsdl:operation name="getSimpleCompanyInfo">
        <wsdl:input message="tns:getSimpleCompanyInfoRequest" />
        <wsdl:output message="tns:getSimpleCompanyInfoResponse" />
    </wsdl:operation>
</wsdl:portType>

<wsdl:binding name="MonitoringControllerBinding" type="tns:MonitoringControllerPortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />

    <wsdl:operation name="getSimpleCompanyInfo">
        <soap:operation soapAction="[domain]/#getSimpleCompanyInfo" style="rpc" />
        <wsdl:input>
            <soap:body use="literal" namespace="[namespaceUri]" />
        </wsdl:input>
        <wsdl:output>
            <soap:body use="literal" namespace="[namespaceUri]" />
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>

<wsdl:service name="MonitoringControllerService">
    <wsdl:port name="MonitoringControllerPort" binding="tns:MonitoringControllerBinding">
        <soap:address location="[serviceUri]" />
    </wsdl:port>
</wsdl:service>

工作服务:

<wsdl:message name="PersonnelInfoRequest">
    <wsdl:part name="PersonnelInfoRequest" type="tns:PersonnelInfoRequest" />
</wsdl:message>
<wsdl:message name="PersonnelInfoResponse">
    <wsdl:part name="PersonnelInfoResponse" type="tns:PersonnelInfoResponse" />
</wsdl:message>

<wsdl:portType name="ReportsControllerPortType">
    <wsdl:operation name="PersonnelInfo">
        <wsdl:input message="tns:PersonnelInfoRequest" />
        <wsdl:output message="tns:PersonnelInfoResponse" />
    </wsdl:operation>
</wsdl:portType>

<wsdl:binding name="ReportsControllerBinding" type="tns:ReportsControllerPortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="PersonnelInfo">
        <soap:operation soapAction="[domain]/#PersonnelInfo" style="rpc" />
        <wsdl:input>
            <soap:body use="literal" namespace="[namespaceUri]" />
        </wsdl:input>
        <wsdl:output>
            <soap:body use="literal" namespace="[namespaceUri]" />
        </wsdl:output>
    </wsdl:operation>
</wsdl:binding>

<wsdl:service name="ReportsControllerService">
    <wsdl:port name="ReportsControllerPort" binding="tns:ReportsControllerBinding">
        <soap:address location="[serviceUri]" />
    </wsdl:port>
</wsdl:service>

编辑 3:

ReportsControllerServiceMonitoringControllerService 扩展 javax.xml.ws.Service 并包含使用的 .wsdl 架构位置和命名空间的定义。如您所见,服务类返回一个 PortType 对象:

/**
 * This class was generated by the JAX-WS RI. JAX-WS RI 2.2.9-b130926.1035 Generated source version: 2.2
 */
@WebServiceClient(name = "ReportsControllerService", targetNamespace = "[namespaceUri]", wsdlLocation = "[wsdlUri]")
public class ReportsControllerService extends Service {

    @WebEndpoint(name = "ReportsControllerPort")
    public ReportsControllerPortType getReportsControllerPort() {
        return super.getPort(new QName("[namespaceUri]", "ReportsControllerPort"), ReportsControllerPortType.class);
    }
}

ReportsControllerPortType 是一个接口,其中包含服务中存在的每个操作端点的方法

/**
 * This class was generated by the JAX-WS RI. JAX-WS RI 2.2.9-b130926.1035 Generated source version: 2.2
 */
@WebService(name = "ReportsControllerPortType", targetNamespace = "[namespaceUri]")
@SOAPBinding(style = SOAPBinding.Style.RPC)
@XmlSeeAlso({ObjectFactory.class})
public interface ReportsControllerPortType {

    @WebMethod(operationName = "PersonnelInfo", action = "[actionName]")
    @WebResult(name = "PersonnelInfoResponse", partName = "PersonnelInfoResponse")
    public PersonnelInfoResponse personnelInfo(
        @WebParam(name = "PersonnelInfoRequest", partName = "PersonnelInfoRequest") PersonnelInfoRequest personnelInfoRequest);
    }
}

问题是,所有这些类都是基于 .wsdl 模式由 JAX-WS(如您从 cmets 中看到的)自动生成的。该实现被抽象在 JDK 内部的某个地方,我也无法控制它。是的,我可以重构代码以便绕过 JAX-WS,但据我了解,这应该是使用基于 .wsdl 的 SOAP 服务的事实上标准方式。

该项目使用 Spring 作为基础框架,我已经确认当我使用 Spring-WS 时这两个服务都可以工作,所以我可以重构,但我想了解为什么这种方式不起作用。

【问题讨论】:

  • 您的问题有点不清楚:您如何调用该服务?您可以同时控制服务和客户端吗?
  • @kolossus,我编辑了我的问题来回答你的问题。
  • ReportsControllerService 是什么,getReportsControllerPort() 中发生了什么?
  • @kolossus,再次编辑。谢谢:)

标签: java web-services soap jax-ws


【解决方案1】:

我相信我可能已经找到了您的网络服务行为不端的原因。根据规范,RPC 网络服务必须在其肥皂绑定中满足以下条件(即您在此处拥有的 @SOAPBinding 注释)

  1. styleRPC:检查

  2. useLITERAL:检查

  3. WRAPPED 中的parameterStyle:从你给出的描述来看,WRAPPED 似乎不是这里的情况,因为你收到的是一个赤裸裸的PersonnelInfoResponse(可能发送一个没有任何包装的裸PersonnelInfoRequest) 表示服务本身已损坏。

摘自the JAX-WS spec

使用RPC 样式需要使用WRAPPED 参数样式。偏离这是一个错误

【讨论】:

  • PersonnelInfoRequest 被包裹在PersonnelInfo 中发送出去,但是是的,结果是一个赤裸裸的PersonnelInfoResponse。从我在 Google 上也能找到的内容来看,这似乎确实是问题所在。谢谢你确认。唉,我无法控制服务,所以我重构了代码并使用 Spring-WS 进行调用。现在可以了。
【解决方案2】:

你的 2 个电话之间有一个明显的区别:

您应该尝试从您的第二个呼叫中删除 operationName = "PersonnelInfo",该呼叫在第一个工作呼叫中不存在。

【讨论】:

  • 这不起作用,因为据我了解,如果它丢失,那么 JAX-WS 会尝试从方法签名中解析操作名称。由于该方法以小写 p 开头,因此它会失败。我用 .wsdl 定义更新了我的原始帖子,您还可以看到它们的结构实际上是相同的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 2016-12-20
  • 2019-07-27
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
相关资源
最近更新 更多