为了在自动生成的JAX-WS的响应XSD上获得不同的元素名,我应该做哪些更改?

为了在自动生成的JAX-WS的响应XSD上获得不同的元素名,我应该做哪些更改?,xsd,jax-ws,Xsd,Jax Ws,我使用JAX-WS用Java创建了一个Web服务。它是一个简单的函数,只返回字符串的大写版本: @WebService(endpointInterface = "mod2.Mod2") public class Mod2Impl implements Mod2 { @Override public String mod2(String x) { return x.toUpperCase(); } } 及其界面: @WebService public

我使用
JAX-WS
用Java创建了一个Web服务。它是一个简单的函数,只返回
字符串的大写版本

@WebService(endpointInterface = "mod2.Mod2")
public class Mod2Impl implements Mod2 {

    @Override
    public String mod2(String x) {

        return x.toUpperCase();

    }
}
及其界面:

@WebService
public interface Mod2 {

    @WebMethod
    String mod2(String x);

}
@WebService
public interface Mod2 {

    @WebMethod
    @WebResult(name="mod2Result")
    String mod2(String x);

}
JAX使用相关类为我生成mod2.jaxws包。答案是这样的:

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

}
部署时,它生成适当的
WSDL
文件,并导入到
XSD
。这是
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>
为了实现这一目标,我必须改变什么

@WebService
public interface Mod2 {

@WebMethod
@XMLElement(name="returnChanged")
String mod2(String x);

}
您可以在web服务中尝试此代码。这是一个伪代码

我找到了答案

我将
@WebResult(name=“mod2Result”)
添加到我的界面:

@WebService
public interface Mod2 {

    @WebMethod
    String mod2(String x);

}
@WebService
public interface Mod2 {

    @WebMethod
    @WebResult(name="mod2Result")
    String mod2(String x);

}
然后再次运行
wsgen
。这产生了以下反应:

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

}
它还有Joshi所说的
@xmlement(name=“mod2Result”)
,但它也更改了变量、setter和getter的名称。我在Response类中直接使用@xmlement,但没有成功