gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:36:52 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的网络服务,它将一个字符串作为输入并返回一个字符串作为输出。我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com