我正在开发一个Web服务,其中有几种方法将相同的复杂数据类型作为输入。数据类型具有JAXB注释和setter和getter,Web服务类具有JAX-WS注释。
我的service.java文件的模板:
@WebService(serviceName = "ServiceWS")
public class SericeWS {
private static ServiceIF serviceImpl;
static {
serviceImpl = new ServiceImpl();
}
public Result Method1(Credentials credentials) {
@WebParam(name = "credentials") Credentials credentials) {
return serviceImpl.Method1(credentials);
}
public Result Method2(Credentials credentials) {
@WebParam(name = "credentials") Credentials credentials) {
return serviceImpl.Method2(credentials);
}
}
编辑:我的Credentials.java文件:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"password"
})
@XmlRootElement(name = "Credentials")
public class Credentials implements MyBean {
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String password;
/**
* Gets the value of the name property.
*
* @return The name property of the credentials
*
*/
public String getName() {
return name;
}
/**
* Sets the value of the name property.
*
* @param value The name property of the credentials
*
*/
public void setName(String value) {
this.name = value;
}
/**
* Gets the value of the password property.
*
* @return The password property of the credentials
*
*/
public String getPassword() {
return password;
}
/**
* Sets the value of the password property.
*
* @param value The password property of the credentials
*
*/
public void setPassword(String password) {
this.password = password;
}
}
该服务部署在Tomcat中,并且自动生成wsdl。当使用wsimport生成客户端存根时,我会获得Credentials类型(Credentials,Method1.Credentials和Method2.Credentials)的重复生成,即每个方法的不同(内部)类。
生成wsdl和schema时似乎存在问题:
<xs:schema xmlns:tns="http://service.my.package.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
targetNamespace="http://service.my.package.com/">
<xs:element name="Credentials">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="password" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
....
<xs:complexType name="getLockBoxKeys">
<xs:sequence>
<xs:element name="credentials" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="password" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
.....
如何才能完成所有这些工作,以便我只有一个凭据定义?我对Web服务,JAX-WS和JAXB都很陌生,所以我不确定我的注释是否合适。
非常感谢任何帮助。
答案 0 :(得分:3)
我不会给出解释所有规则的完整答案,因为我不记得/了解所有规则。
但我认为,如果您将name
元素添加到@XMLType
注释中,您将获得所需内容(或者至少更进一步)。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Credentials", propOrder = {
"name",
"password"
})
@XmlRootElement(name = "Credentials")
public class Credentials {
顺便说一句,您原来的service.java文件似乎没有太干净地粘贴(我认为有一些不正确的括号)使得任何人都难以重新创建。
答案 1 :(得分:1)
在MyCredentials.java文件中,@ XmlRootElement(name =“Credentials”)的名称以大写字母C开头,而@WebParam(name =“credentials”)则不是。也许是因为这个?