Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xsd

jax-ws

I have created a Web Service in Java with JAX-WS. It is a simple one that just returns an uppercased version of a 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 relevant classes. The response is like this:

@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 proper WSDL file with an import to an XSD. This is the 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, what I want is to change the element named "return" in the XSD for whatever I want. I have tried changing the @XmlElement(name = "return", namespace = "") in the Mod2Response class but this throws 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 is it I have to change to achieve this?

like image 329
eskalera Avatar asked Jan 13 '23 09:01

eskalera


1 Answers

I have 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 the wsgen again. Which generated the following Response:

@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 the @XmlElement(name = "mod2Result") as stated by Joshi but it also changed the name of variable, setter and getter. I tried with the @XmlElement straight in the Response class only with no success.

like image 65
eskalera Avatar answered Feb 13 '23 14:02

eskalera