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

时间:2011-08-08 15:41:06

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

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

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

    新增 - >代码生成器 - >来自wsdl->的java类给出WSDL并生成java骨架。

  3.   
  4. 在谢尔顿中,我只是打印出来的值作为参数。并返回相同的值。
  5.   
  6. 我编写了客户端代码来调用webservice的方法。这需要一个字符串。
  7.   
  8. 但是当我尝试调用该方法时,我遇到异常并且它没有访问web服务。
  9.   

    事实上我正在使用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 desirialization的一些问题。尽管SOVO在类路径中为什么会发生?我错过了什么吗?

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

      

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

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

      

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

    我不确定是什么问题。

1 个答案:

答案 0 :(得分:2)

我建议如下:

使用其他客户端测试服务

使用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消息。密切关注在您的开始标记之前出现的内容,如错误消息所示。