2015-04-08 23 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