Java Webservices and SOAP - Changing an element name
element, java, soap, web-services
Solution
You'll need to make sure `myType` is annotated with `@XmlRootElement(name="myType")`. (You might need to annotate the method with `@WebResult(name="myType")` too.
(In Java, class names start with an uppercase letter so it should really be `MyType`)
Problem
I'm writing a java web service that returns a custom type. Everything works fine except when I look at the SOAP response it doesn't use the name "myType" - it uses "return" This is my SOAP response - basically where it says "return", I want it to say "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> ``` Class package 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 class ``` 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; } } ``` SOLUTION ``` 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; } } ```