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

发布于 2024-11-28 15:03:20 字数 8298 浏览 5 评论 0原文

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

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

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

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

事实上,我正在将 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>

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

我不太确定它是否触及 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 作为字符串发送时,它显示:

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

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

在开始标记之前只允许使用空白内容,而不允许使用 s(位置:START_DOCUMENT 可见...@1:1)

我不确定出了什么问题。

I am trying to create a simple webservice which takes a String as input and returns string as output.
I am using Ecelipse Helios and Axis 2.15.

  1. I am writing simple WSDL for the same.
  2. I am generating the stubs using code generator.

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

  3. And in the skelton I am just print the value what is coming as parameter. and returning the same value.
  4. I have written client code to invoke the method of the webservice. which takes a String.
  5. but when I am trying to invoke the method I am getting following exception and it's not hitting the webservice.

Infact I am using XStream along with Client/WebService.

Code goes like this for the webservice skeleton:

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;
}

My client code goes like this :

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 is a simple POJO which is present at both client and webservice side.

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;
    }

}

And last but most important WSDL is here:

<?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>

And the Stack trace for the exception I must modify :

I am not very sure if its hitting the webservice layer.

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)

It looks like more its some problem with XStream desirialization. Even though SOVO is in the classpath why its happening? Am I missing something?

When I try sending XXXXX as string it tells:

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

When i try sending "some value" it says:

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

I am not sure what's wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2024-12-05 15:03:20

我建议如下:

与另一个客户端测试服务

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

I would suggest the following:

Test the service with another client

Use soapUI to generate a valid test request for your showInput method. If you don't get any errors using this tool, you know your service is working fine. If you do get an error, then you know to start digging around in your service code.

Enable client side logging for SOAP messages

Add these JVM options when running your client:

-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

This will let you to see the SOAP messages that get transmitted. Pay close attention to content appearing before your start tags like the error message says.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文