java - 使用 XStream 通过线路发送的 String 的反序列化问题

标签 java web-services xml-serialization axis2 xstream

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

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

    New ->code generator -> java class from wsdl-> give WSDL and generates the java skeletons.

  3. 在 skelton 中,我只是打印作为参数出现的值。并返回相同的值。
  4. 我已经编写了客户端代码来调用网络服务的方法。它需要一个字符串。
  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,存在于客户端和网络服务端。

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>

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

我不太确定它是否命中了 web 服务层。

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 作为字符串发送时,它会告诉:

only whitespace content allowed before start tag and not X (position: START_DOCUMENT seen X... @1:1)

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

only whitespace content allowed before start tag and not s (position: START_DOCUMENT seen s... @1: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 消息。请密切注意出现在开始标记之前的内容,如错误消息所述。

关于java - 使用 XStream 通过线路发送的 String 的反序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6984707/

相关文章:

java - 如何使用 XStream 反序列化此 xml?

java - 在 Kotlin 中解析列表的枚举

java - NoSuchBeanDefinition异常 : No qualifying bean of type [Repository] found for dependency: expected at least 1 bean which qualifies as autowire

java - 从 JAX-WS 生成的代码访问 Web 服务时如何指定主机和端口?

linux - 我正在寻找一个在 FreeBSD 上托管 Web 服务的应用程序

java - 如何创建简单的网络服务

java - "Loop unswitching"优化不起作用

java - Spring 启动 JPA : Many to Many relation as its own class?

java - 如何以内存高效的方式将 xml 写入 bean 网络流

c# - 如何使用 XmlSerializer 将字符串序列化为 CDATA?