Java Webservices和SOAP – 更改元素名称

我正在编写一个返回自定义类型的java Web服务。 一切正常,除非我查看SOAP响应时它不使用名称“myType” – 它使用“return”

这是我的SOAP响应 – 基本上它表示“返回”,我希望它说“mytype”

S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">    sdf sdf     

类包myWebserivce

 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; } } 

您需要确保myType @XmlRootElement(name="myType")注释@XmlRootElement(name="myType") 。 (您可能还需要使用@WebResult(name="myType")注释该方法。

(在Java中,类名以大写字母开头,因此它应该是MyType