What should I change to get a different element name in the XSD response of automatically generated JAX-WS?

I created a Java web service with JAX-WS . This is a simple one that just returns a version labeled String :

 @WebService(endpointInterface = "mod2.Mod2") public class Mod2Impl implements Mod2 { @Override public String mod2(String x) { return x.toUpperCase(); } } 

and its interface:

 @WebService public interface Mod2 { @WebMethod String mod2(String x); } 

JAX generates the mod2.jaxws package for me with the appropriate classes. The answer is:

 @XmlRootElement(name = "mod2Response", namespace = "http://mod2/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "mod2Response", namespace = "http://mod2/") public class Mod2Response { @XmlElement(name = "return", namespace = "") private String _return; /** * * @return * returns String */ public String getReturn() { return this._return; } /** * * @param _return * the value for the _return property */ public void setReturn(String _return) { this._return = _return; } } 

When deployed, it generates the correct WSDL file with import to XSD . This is XSD :

 <xs:schema xmlns:tns="http://mod2/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://mod2/"> <xs:element name="mod2" type="tns:mod2"/> <xs:element name="mod2Response" type="tns:mod2Response"/> <xs:complexType name="mod2"> <xs:sequence> <xs:element name="arg0" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="mod2Response"> <xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:schema> 

Now I want to change the element named "return" in XSD for what I want. I tried changing @XmlElement(name = "return", namespace = "") in the Mod2Response class, but this causes the following error:

 GRAVE: WSSERVLET11: failed to parse runtime descriptor: javax.xml.ws.WebServiceException: class mod2.jaxws.Mod2Response do not have a property of the name return 

What should I change to achieve this?

+4
source share
2 answers

I found the answer here .

I added @WebResult(name="mod2Result") to my interface:

 @WebService public interface Mod2 { @WebMethod @WebResult(name="mod2Result") String mod2(String x); } 

and then run wsgen again. Which caused the following answer:

 @XmlRootElement(name = "mod2Response", namespace = "http://mod2/") @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "mod2Response", namespace = "http://mod2/") public class Mod2Response { @XmlElement(name = "mod2Result", namespace = "") private String mod2Result; /** * * @return * returns String */ public String getMod2Result() { return this.mod2Result; } /** * * @param mod2Result * the value for the mod2Result property */ public void setMod2Result(String mod2Result) { this.mod2Result = mod2Result; } } 

which also has @XmlElement(name = "mod2Result") , as indicated by Joshi, but also changed the variable name, setter and getter. I tried with @XmlElement directly in the Response class only without success.

+6
source
 @WebService public interface Mod2 { @WebMethod @XMLElement(name="returnChanged") String mod2(String x); } 

You can try this code in your web service. This is pseudo code.

0
source

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


All Articles