2014-10-26 79 views
0

我有一個連接到SOAP服務器端點的客戶端:SOAP服務器讀取爲空的Web方法參數

Endpoint.publish("http://localhost:8081/todo", new ToDoWebService()); 

服務器提供下一個簡單的方法:

@WebService 
public class ToDoWebService { 

    @WebMethod() 
    public String addToDo(String task, String context, String project, int priority) { 
     return "ToDo: \n " 
       + "\t Task : "+task+"\n" 
       + "\t Context : "+context+"\n" 
       + "\t Project : "+project+"\n" 
       + "\t Priority: "+priority; 
    } 
} 

這是客戶端

public class Client { 

    public static void main(String[] args) { 
     ToDoWebServiceService tdwss = new ToDoWebServiceService(); 
     ToDoWebService tdws = tdwss.getToDoWebServicePort(); 

     System.out.println(tdws.addToDo("Task 1","My Context","My Project",9)); 
    } 
} 

問題是客戶端和服務器之間的連接是成功的,但不是完全的:從客戶端到服務的參數傳遞並非如預期般進行,這是客戶端執行後的結果:

enter image description here

服務器未正確接收客戶端通過方法「addToDo()」調用傳遞的參數。它返回預期的字符串格式,但用空值代替客戶端傳遞的參數。而這正是我想不通......

當然,我是非常有信心的WSDL文件寫得很好:

<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --> 
    <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://todows.bigws/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://todows.bigws/" name="ToDoWebServiceService"> 
     <types> 
     <xsd:schema> 
      <xsd:import namespace="http://todows.bigws/" schemaLocation="toDo.xsd"/> 
     </xsd:schema> 
     </types> 
     <message name="addToDo"> 
      <part name="parameters" element="tns:addToDo"/> 
     </message> 
     <message name="addToDoResponse"> 
      <part name="parameters" element="tns:addToDoResponse"/> 
     </message> 
     <portType name="ToDoWebService"> 
      <operation name="addToDo"> 
       <input wsam:Action="http://todows.bigws/ToDoWebService/addToDo" message="tns:addToDo"/> 
       <output wsam:Action="http://todows.bigws/ToDoWebService/addToDoResponse" message="tns:addToDoResponse"/> 
      </operation> 
     </portType> 
     <binding name="ToDoWebServicePortBinding" type="tns:ToDoWebService"> 
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> 
      <operation name="addToDo"> 
       <soap:operation soapAction=""/> 
       <input> 
        <soap:body use="literal"/> 
       </input> 
       <output> 
        <soap:body use="literal"/> 
       </output> 
      </operation> 
     </binding> 
     <service name="ToDoWebServiceService"> 
      <port name="ToDoWebServicePort" binding="tns:ToDoWebServicePortBinding"> 
       <soap:address location="http://localhost:8081/todo"/> 
      </port> 
     </service> 
    </definitions> 

還有模式「TODO。 XSD」

<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.8 svn-revision#13980. --> 
<xs:schema xmlns:tns="http://todows.bigws/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://todows.bigws/"> 

    <xs:element name="addToDo" type="tns:addToDo"/> 

    <xs:element name="addToDoResponse" type="tns:addToDoResponse"/> 

    <xs:complexType name="addToDo"> 
     <xs:sequence> 
      <xs:element name="task" type="xs:string" minOccurs="0"/> 
      <xs:element name="context" type="xs:string" minOccurs="0"/> 
      <xs:element name="project" type="xs:string" minOccurs="0"/> 
      <xs:element name="priority" type="xs:int" minOccurs="0"/> 
     </xs:sequence> 
    </xs:complexType> 

    <xs:complexType name="addToDoResponse"> 
     <xs:sequence> 
      <xs:element name="return" type="xs:string" minOccurs="0"/> 
     </xs:sequence> 
    </xs:complexType> 
</xs:schema> 

我已經試過在服務器方法不同的動作,如寫在一個文件中的參數字符串,但結果是一樣的,錯誤原因參數是null。這就是我認爲失敗的原因在於服務器接收傳遞給「addToDo()」方法的參數。

我已經反覆檢查了WSDL &模式文件....但是我一直無法找到導致此代碼運行錯誤的原因。我還用SoapUI軟件測試了這個代碼,結果是一樣的。 ¿有什麼想法?

P.D .:抱歉所有發佈的代碼。

如果您需要整個項目:my SOAP project at GitHub

+1

你能提供一些鏈接到你的代碼嗎?我想重現這個問題。 – 2014-10-27 00:28:37

+0

當您使用[SoapUI](http://www.soapui.org/)進行測試時,行爲是否相同?即您發送(「任務1」,「我的上下文」,「我的項目」,9),並且服務器將消息解組爲(null,null,null,0) – 2014-10-27 00:32:26

+1

完成,添加一個包含所有代碼的鏈接。 是的,與SoapUI完全相同的行爲。 @ FranciscoJ.Lopez-Pellicer – daniegarcia254 2014-10-27 07:22:06

回答

3

我檢查了您的WSDL和XSD在您的項目todows-cli-ws,問題是在客戶端項目的WSDL和XSD文件已經過時

例如,這是服務器的XSD文件的當前內容。

<xs:schema xmlns:tns="http://todows.bigws/" xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    version="1.0" targetNamespace="http://todows.bigws/"> 
<xs:element name="addToDo" type="tns:addToDo"/> 
<xs:element name="addToDoResponse" type="tns:addToDoResponse"/> 
<xs:element name="listToDo" type="tns:listToDo"/> 
<xs:element name="listToDoResponse" type="tns:listToDoResponse"/> 
<xs:element name="removeToDo" type="tns:removeToDo"/> 
<xs:element name="removeToDoResponse" type="tns:removeToDoResponse"/> 
<xs:complexType name="addToDo"> 
    <xs:sequence> 
    <xs:element name="arg0" type="xs:string" minOccurs="0"/> 
    <xs:element name="arg1" type="xs:string" minOccurs="0"/> 
    <xs:element name="arg2" type="xs:string" minOccurs="0"/> 
    <xs:element name="arg3" type="xs:int"/> 
    </xs:sequence> 
</xs:complexType> 
<xs:complexType name="addToDoResponse"> 
    <xs:sequence> 
    <xs:element name="return" type="xs:string" minOccurs="0"/> 
    </xs:sequence> 
</xs:complexType> 
<xs:complexType name="removeToDo"> 
    <xs:sequence/> 
</xs:complexType> 
<xs:complexType name="removeToDoResponse"> 
    <xs:sequence> 
    <xs:element name="return" type="xs:string" minOccurs="0"/> 
    </xs:sequence> 
</xs:complexType> 
<xs:complexType name="listToDo"> 
    <xs:sequence/> 
</xs:complexType> 
<xs:complexType name="listToDoResponse"> 
    <xs:sequence> 
    <xs:element name="return" type="xs:string" minOccurs="0"/> 
    </xs:sequence> 
</xs:complexType> 
</xs:schema> 

注意,則params的當前名稱是爲arg0ARG1ARG2ARG3。服務器期望爲PARAM名任務方面項目優先,因此該方法被調用,有默認值(字符串和爲INT )。

+0

非常感謝!這就是問題所在,我已將客戶端** _。XSD _ **文件中的參數名更新爲_arg0,arg1 ..._,並且完美地工作。 – daniegarcia254 2014-10-27 10:26:18