Java Webservices and SOAP - Element Name Change

I am writing a java web service that returns a custom type. Everything works fine, except when I look at the SOAP answer, it does not use the name "myType" - it uses "return"

This is my SOAP answer - 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; } } 

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

DECISION

 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; } } 
+4
source share
1 answer

You need to make sure myType annotated with @XmlRootElement(name="myType") . (You may also need to annotate the method with @WebResult(name="myType") .

(In Java, class names begin with a capital letter, so it should be myType )

+4
source

Source: https://habr.com/ru/post/1439110/


All Articles