Java 使用XStream通过线路发送的字符串的反序列化问题

Java 使用XStream通过线路发送的字符串的反序列化问题,java,web-services,xml-serialization,axis2,xstream,Java,Web Services,Xml Serialization,Axis2,Xstream,我正在尝试创建一个简单的Web服务,它以字符串作为输入并返回字符串作为输出。 我使用的是Ecelipse Helios和Axis 2.15 我正在为此编写简单的WSDL 我正在使用代码生成器生成存根。 新建->代码生成器->来自wsdl的java类->提供wsdl并生成java框架 在skelton中,我只是打印出作为参数的值。并返回相同的值 我已经编写了客户端代码来调用webservice的方法。这需要一个字符串 但是当我试图调用该方法时,我得到了以下异常,并且它没有命中Web服务 事实上,我

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

  • 我正在为此编写简单的WSDL
  • 我正在使用代码生成器生成存根。 新建->代码生成器->来自wsdl的java类->提供wsdl并生成java框架
  • 在skelton中,我只是打印出作为参数的值。并返回相同的值
  • 我已经编写了客户端代码来调用webservice的方法。这需要一个字符串
  • 但是当我试图调用该方法时,我得到了以下异常,并且它没有命中Web服务
  • 事实上,我正在使用XStream和客户机/Web服务

    webservice框架的代码如下所示:

    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>
    
    看起来更像是XStream理想化的问题。即使SOVO在类路径中,为什么会发生这种情况?我错过什么了吗

    当我尝试以字符串形式发送XXXXX时,它会告诉我:

    仅允许在开始标记之前使用空白内容,而不允许使用X(位置:开始位置X..@1:1)

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

    仅允许在开始标记之前使用空格内容,而不允许在s之前使用空格内容(位置:start_文档s..@1:1)


    我不确定出了什么问题。

    我建议如下:

    使用其他客户端测试服务

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

    您可以发布WSDL和一些代码吗?正如@Thomas所说,WSDL将非常好。。。客户机代码我建议通过使用web服务浏览器(运行->底部项目..我假设您使用的是Eclipse)测试您的web服务,以确保其正常工作。我将在回复中发布客户机代码、框架和wsdl。bcoz注释将太短。感谢您回来评论Thomas/bdares。
    -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