我正在编写一个返回自定义类型的 java web 服务。一切正常,除了当我查看 SOAP 响应时它不使用名称“myType” - 它使用“return”
这是我的 SOAP 响应 - 基本上它说“return”,我希望它说“mytype”
S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:MethodResponse xmlns:ns2="http://myWebservice/">
<return>
<field1>sdf</field1>
<field2>sdf</field2>
</return>
</ns2:MethodResponse >
</S:Body>
</S:Envelope>
类包 myWebservice
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "myWebserivce")
public class myWebserivce{
@WebMethod(operationName = "Method")
public MyType Method(@WebParam(name = "string1") String string1, @WebParam(name = "string2") String string2) {
MyType mt = new MyType();
mt.setField1(string1);
mt.setfield2(string2);
return mt;
}
}
MyType 类
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="MyType")
public class MyType {
private String field1;
private String field2;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
}
解决方案
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "myWebserivce")
public class myWebserivce{
@WebMethod(operationName = "Method")
@WebResult(name="MyType")
public MyType Method(@WebParam(name = "string1") String string1, @WebParam(name = "string2") String string2) {
MyType mt = new MyType();
mt.setField1(string1);
mt.setfield2(string2);
return mt;
}
}