我用Java创建了一个Web服务JAX-WS.这是一个简单的只返回a的大写版本String:
@WebService(endpointInterface = "mod2.Mod2")
public class Mod2Impl implements Mod2 {
@Override
public String mod2(String x) {
return x.toUpperCase();
}
}
Run Code Online (Sandbox Code Playgroud)
及其界面:
@WebService
public interface Mod2 {
@WebMethod
String mod2(String x);
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
部署后,它会生成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>
Run Code Online (Sandbox Code Playgroud)
现在,我想要的是在XSD中为我想要的任何东西更改名为"return"的元素.我尝试更改@XmlElement(name = "return", namespace = "")Mod2Response类,但这会引发以下错误:
GRAVE: WSSERVLET11: failed to parse runtime descriptor: javax.xml.ws.WebServiceException: class mod2.jaxws.Mod2Response do not have a property of the name return
Run Code Online (Sandbox Code Playgroud)
为了达到这个目的,我必须改变什么?
我在这里找到了答案.
我添加@WebResult(name="mod2Result")到我的界面:
@WebService
public interface Mod2 {
@WebMethod
@WebResult(name="mod2Result")
String mod2(String x);
}
Run Code Online (Sandbox Code Playgroud)
然后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;
}
}
Run Code Online (Sandbox Code Playgroud)
它也有@XmlElement(name = "mod2Result")Joshi所述,但它也改变了变量,setter和getter的名称.我在Response类中直接尝试使用@XmlElement但没有成功.