java - JAX-WS 在生成 wsdl 时复制复杂类型

标签 java web-services jaxb wsdl jax-ws

我正在开发一个 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 生成客户端 stub 时,我得到了 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 很陌生,所以我不确定我的注释是否正确。

如有任何帮助,我们将不胜感激。

最佳答案

我不会给出解释所有规则的完整答案,因为我不记得/理解所有规则。

但我认为如果您将 name 元素添加到您的 @XMLType 注释中,您将获得您正在寻找的内容(或至少更进一步)。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Credentials", propOrder = {
    "name",
    "password"
})
@XmlRootElement(name = "Credentials")
public class Credentials {

顺便说一句,您的原始 service.java 文件似乎没有粘贴得太干净(我认为有一些不正确的括号)让任何人都很难重新创建。

关于java - JAX-WS 在生成 wsdl 时复制复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16078420/

相关文章:

java - 如何在两个测试类之间共享变量?

java - 解码多个元素的同名对象

jaxb - 想要在使用 JAXB 验证 XML 时获取所有错误列表

java - 什么是好的 Java 网络搜索和网络爬虫引擎?

java.sql.SQLException : null, 来自服务器的消息: "Host ' xxx' 不允许连接到此 MySQL 服务器”

java - org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.NoUniqueBeanDefinitionException

web-services - JAX-WS Web 服务和 @rolesAllowed

web-services - 为什么 Dynamics AX 客户端在部署服务组后停止工作?

java - Apache CXF Web 服务上的 SOAP 命名空间问题

xsd - 使用 trang 将 RELAX NG 转换为 XSD,同时保留外部命名空间元素(用于 JAXB)