如何在Java(javax.jws)中使用SOAP返回多个值
我将Java8和Eclipse与Tomcat8一起使用。 我想编写一个SOAP web服务,其中返回3个整数,每个整数都有一个不同的字段名(id、键和值),如下所示:如何在Java(javax.jws)中使用SOAP返回多个值,java,web-services,soap,Java,Web Services,Soap,我将Java8和Eclipse与Tomcat8一起使用。 我想编写一个SOAP web服务,其中返回3个整数,每个整数都有一个不同的字段名(id、键和值),如下所示: <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/XMLS
<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>
它返回:
<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>
1.
1.
2.
但我不知道,也不知道如何将文件名从getArrReturn更改为id或key
编辑:
我试图返回哈希表对象,但它返回:
<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>
钥匙
1.
价值
2.
身份证件
1.
您应该尝试创建并返回一个带有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生成此类更方便,如果要创建更复杂的服务,请考虑这一点。您是否想过返回自定义对象而不是数组?我试图返回一个哈希表。但是“自定义对象”是什么意思呢?创建一个包含3个整型字段(key、value、id)的类并返回一个该类的对象i不起作用,我有一个错误(return是我的对象的名称):java.io.IOException:MapSerializer:return不是一个映射感谢您的响应。但是当我想要返回一个自定义对象“java.io.IOException:MapSerializer:return不是一个映射”(return是我的对象的名称)时,我仍然有相同的错误:/
@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;
}