2011-08-08 17 views
3

我想創建一個簡單的webservice,它將一個字符串作爲輸入並返回字符串作爲輸出。 我使用Ecelipse Helios和Axis 2.15。使用XStream通過電線發送字符串的反序列化問題

  1. 我正在爲此編寫簡單的WSDL。
  2. 我正在使用代碼生成器生成存根。

    新建 - >代碼生成器 - > java類從wsdl->給WSDL並生成java骨架。

  3. 而在skelton中,我只是將值作爲參數打印出來。並返回相同的值。
  4. 我寫了客戶端代碼來調用webservice的方法。這需要一個字符串。
  5. 但當我試圖調用該方法時,我得到以下異常,它不是擊中web服務。

Infact我正在使用XStream以及客戶端/ 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 desirialization更多的一些問題。即使SOVO在classpath中爲什麼會發生?我錯過了什麼嗎?

當我嘗試發送XXXXX爲字符串,它告訴:之前開始標記,而不是X

只有空白內容允許(位置:START_DOCUMENT看到X ... @ 1:1)

當我嘗試發送「一些價值」,它說:之前開始標記和不發

只有空白內容允許(位置:START_DOCUMENT看到小號... @ 1:1)

我不確定有什麼問題。

+2

你可以發佈WSDL和一些代碼嗎? – Thomas

+0

正如@Thomas所說,WSDL將會很棒......客戶端代碼以及 – home

+0

我建議通過使用Web服務資源管理器測試它,以確保您的Web服務正常工作(運行 - >底部項目..您使用的是Eclipse我會承擔)。 – bdares

回答

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消息。密切關注在您的開始標記之前出現的內容,例如錯誤消息所述。