【问题标题】:Problem with deserialization of String sent over wire with XStream使用 XStream 通过线路发送的字符串的反序列化问题
【发布时间】:2011-10-22 12:26:54
【问题描述】:

我正在尝试创建一个简单的 web 服务,它将字符串作为输入并返回字符串作为输出。 我正在使用 Ecelipse Helios 和 Axis 2.15。

  1. 我正在为此编写简单的 WSDL。
  2. 我正在使用代码生成器生成存根。

    新建 -> 代码生成器 -> 来自 wsdl 的 java 类-> 提供 WSDL 并生成 java 骨架。

  3. 在骨架中,我只是打印作为参数传入的值。并返回相同的值。
  4. 我已经编写了客户端代码来调用 Web 服务的方法。它需要一个字符串。
  5. 但是当我尝试调用该方法时,我遇到了以下异常并且它没有访问 Web 服务。

事实上,我将 XStream 与 Client/WebService 一起使用。

Web 服务框架的代码如下所示:

public com.url.pkg.ShowInputResponse showInput(
        com.url.pkg.ShowInput showInput) {
    // TODO : fill this with the necessary business logic
    String inputString = showInput.getInputString();
    System.out.println("INput String is :\n" + inputString);
    XStream xStream = new XStream();
    System.out.println("After XStream Declaration...");
    SOVO vo = null;
    try {
        vo = (SOVO) xStream.fromXML(inputString);
    } catch (Throwable e) {
        System.out.println(e);
        e.printStackTrace();
    }
    System.out.println("After SOVO casting from XML");
    System.out.println(vo.getName());
    System.out.println(vo.getParams());
    // TODO: business logic
    ShowInputResponse response = new ShowInputResponse();
    response.set_return(inputString);
    return response;
}

我的客户端代码是这样的:

public static void main(String[] args) throws Exception {
        BasicServiceStub stub = new BasicServiceStub();
        ShowInput request = new ShowInput();
        SOVO sovo = new SOVO();
        sovo.setName("I am the post for SO");
        Map params = new HashMap();
        params.put("key1", "val1");
        params.put("key2", "val2");
        sovo.setParams(params);
        XStream xStream = new XStream();
        String soVoString = xStream.toXML(sovo);
        // System.out.println(soVoString);
        request.setInputString(soVoString);
        ShowInputResponse response = stub.showInput(request);
        System.out.println("....................................");
        System.out.println("response = " + response.get_return());
    }

SOVO 是一个简单的 POJO,它同时存在于客户端和 Web 服务端。

public class SOVO {

    private String name;
    private Map params;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map getParams() {
        return params;
    }

    public void setParams(Map params) {
        this.params = params;
    }

}

最后但最重要的 WSDL 在这里:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://pkg.url.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://pkg.url.com">
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://pkg.url.com">
            <xs:element name="showInput">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="inputString" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="showInputResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="showInputRequest">
        <wsdl:part name="parameters" element="ns:showInput"/>
    </wsdl:message>
    <wsdl:message name="showInputResponse">
        <wsdl:part name="parameters" element="ns:showInputResponse"/>
    </wsdl:message>
    <wsdl:portType name="BasicServicePortType">
        <wsdl:operation name="showInput">
            <wsdl:input message="ns:showInputRequest" wsaw:Action="urn:showInput"/>
            <wsdl:output message="ns:showInputResponse" wsaw:Action="urn:showInputResponse"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="BasicServiceSoap11Binding" type="ns:BasicServicePortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="showInput">
            <soap:operation soapAction="urn:showInput" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="BasicServiceSoap12Binding" type="ns:BasicServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdl:operation name="showInput">
            <soap12:operation soapAction="urn:showInput" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="BasicServiceHttpBinding" type="ns:BasicServicePortType">
        <http:binding verb="POST"/>
        <wsdl:operation name="showInput">
            <http:operation location="BasicService/showInput"/>
            <wsdl:input>
                <mime:content type="text/xml" part="showInput"/>
            </wsdl:input>
            <wsdl:output>
                <mime:content type="text/xml" part="showInput"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="BasicService">
        <wsdl:port name="BasicServiceHttpSoap11Endpoint" binding="ns:BasicServiceSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/BasicService"/>
        </wsdl:port>
        <wsdl:port name="BasicServiceHttpSoap12Endpoint" binding="ns:BasicServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/BasicService"/>
        </wsdl:port>
        <wsdl:port name="BasicServiceHttpEndpoint" binding="ns:BasicServiceHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/BasicService"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

我必须修改异常的堆栈跟踪:

我不太确定它是否击中了网络服务层。

Caused by: org.apache.axis2.AxisFault: string
    at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:446)
    at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:371)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:417)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
        at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
        at com.url.pkg.BasicServiceStub.showInput(BasicServiceStub.java:184)
        at com.url.pkg.Client.main(Client.java:30)

看起来更像是 XStream 反序列化的一些问题。即使 SOVO 在类路径中,为什么它会发生?我错过了什么吗?

当我尝试将 XXXXX 作为字符串发送时,它会告诉您:

在开始标记之前只允许空白内容,而不是 X(位置:START_DOCUMENT 看到 X...@1:1)

当我尝试发送“一些价值”时,它会说:

在开始标记之前只允许空白内容,而不是 s(位置:START_DOCUMENT 看到 s...@1:1)

我不知道出了什么问题。

【问题讨论】:

  • 你能发布 WSDL 和一些代码吗?
  • 正如@Thomas 所说,WSDL 会很棒......客户端代码也是如此
  • 我建议通过使用 Web 服务资源管理器(运行 -> 底部项目。我假设您使用的是 Eclipse)来测试您的 Web 服务是否正常工作。
  • 我将在回复中发布客户端代码、骨架和 wsdl,因为 bcoz 评论太短了。感谢您回来评论 Thomas/bdares。

标签: java web-services xml-serialization axis2 xstream


【解决方案1】:

我建议如下:

与其他客户一起测试服务

使用soapUI 为您的showInput 方法生成有效的测试请求。如果您使用此工具没有收到任何错误,则说明您的服务运行良好。如果您确实遇到错误,那么您就知道该开始挖掘您的服务代码了。

为 SOAP 消息启用客户端日志记录

在运行客户端时添加这些 JVM 选项:

-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
-Dorg.apache.commons.logging.simplelog.showdatetime=true
-Dorg.apache.commons.logging.simplelog.log.httpclient.wire=debug
-Dorg.apache.commons.logging.simplelog.log.org.apache.commons.httpclient=debug

这将让您看到传输的 SOAP 消息。密切注意出现在您的开始标签之前的内容,如错误消息所述。

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 2016-04-12
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2012-06-09
    • 2011-04-01
    相关资源
    最近更新 更多