2015-04-08 21 views
0

我使用Java 8和Eclipse和Tomcat 8 我想寫一個SOAP Web服務至極回報3與他們每個人的不同字段名的整數( ID,key和value)是這樣的:如何與SOAP在Java中返回多個值(javax.jws中)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getArrResponse xmlns="http://DefaultNamespace"> 
     <id>1</id> 
     <key>1</key> 
     <value>2</value> 
     </getArrResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

我寫在Java中這個SOAP服務器和它的作品:

@WebService() 
public class MyWebService { 

    @WebMethod(operationName = "printName") 
    public String printName(@WebParam(name = "userName") String userName) { 

     return "hello " + userName; 
    } 

    @WebMethod 
    public int[] getArr() { 
     int[] i = { 1, 1, 2}; 
     return i; 
    } 
} 

返回:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getArrResponse xmlns="http://DefaultNamespace"> 
     <getArrReturn>1</getArrReturn> 
     <getArrReturn>1</getArrReturn> 
     <getArrReturn>2</getArrReturn> 
     </getArrResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

但我不知道,我沒有找到如何從getArrReturn變更申請名稱ID關鍵

編輯: 我試圖返回一個Hashtable對象,和它返回:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getArrResponse xmlns="http://DefaultNamespace"> 
     <getArrReturn> 
      <item xmlns:ns1="http://xml.apache.org/xml-soap" xmlns=""> 
       <key xsi:type="xsd:string">key</key> 
       <value xsi:type="xsd:int">1</value> 
      </item> 
      <item xmlns=""> 
       <key xsi:type="xsd:string">value</key> 
       <value xsi:type="xsd:int">2</value> 
      </item> 
      <item xmlns=""> 
       <key xsi:type="xsd:string">id</key> 
       <value xsi:type="xsd:int">1</value> 
      </item> 
     </getArrReturn> 
     </getArrResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 
+0

你有沒有想過返回一個自定義對象,而不是一個數組的? – azraelAT

+0

我試着返回一個HashTable。但你對「自定義對象」意味着什麼? – Army

+1

創建具有3個整數字段(鍵,值,id)的一類,並返回該類 – azraelAT

回答

0

你應該嘗試創建和JAXB註釋這樣返回一個類:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "context", 
    "key", 
    "value" 
}) 
@XmlRootElement(name = "getArrResponse") 
public class GetArrResponse implements Serializable 
{ 
    private final static long serialVersionUID = 1L; 
    @XmlElement(name = "id", required = true) 
    private int id; 
    @XmlElement(name = "key", required = true) 
    private int key; 
    @XmlElement(name = "value", required = true) 
    private int value; 
} 

通常它是更方便的從XSD生成這樣的類,考慮這個,如果你要創建更復雜的服務。

+0

感謝您的回覆。但我仍然同樣的錯誤時,我想返回一個自定義對象「產生java.io.IOException:MapSerializer:回報是不是地圖」(回報是我對象的名稱):/ – Army