title: Java Webservices and SOAP - Changing an element name
body: <p>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"</p>
  
  <p>This is my SOAP response - basically where it says "return",  I want it to say  "mytype" </p>
  
  <pre><code>S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
     &lt;S:Body&gt;
        &lt;ns2:MethodResponse xmlns:ns2="http://myWebservice/"&gt;
           &lt;return&gt;
              &lt;field1&gt;sdf&lt;/field1&gt;
              &lt;field2&gt;sdf&lt;/field2&gt;
         &lt;/return&gt;
        &lt;/ns2:MethodResponse &gt;
     &lt;/S:Body&gt;
  &lt;/S:Envelope&gt;
  </code></pre>
  
  <p>Class
  package myWebserivce</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>MyType class</p>
  
  <pre><code>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;
      }
  
  
  }
  </code></pre>
  
  <p>SOLUTION</p>
  
  <pre><code>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;
      }
  }
  </code></pre>