6
votes

I'm developing a web service with several methods taking as input identical complex data types. The data types have JAXB annotations and setters and getters, and the web service class has JAX-WS annotations.

Template of my service.java file:

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

}

EDIT: My Credentials.java file:

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

The service is deployed in Tomcat and the wsdl is auto-generated. When generating the client stubs with wsimport I get duplicate generation of the Credentials type (Credentials, Method1.Credentials and Method2.Credentials), i.e. a different (inner) class for each method.

It seems that the problem arrises when the wsdl and schema are generated :

<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>    
 .....

How can I make all this work such that I have only one definition of Credentials? I am quite new to web services, JAX-WS and JAXB so I'm not sure I have the annotations right.

Any help would be greatly appreciated.

2
Show us the JAXB annotated classes.acdcjunior
Thanks for your answer. I've edited the original question to add the Credentials.java file. Let me know if you want to see anything else.juniz

2 Answers

3
votes

I'm not going to give a complete answer explaining all the rules, since I don't remember/understand them all.

But I think if you add the name element to your @XMLType annotation you'll get what you're looking for (or at least a bit further).

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

BTW, your original service.java file didn't seem to paste too cleanly (had some incorrect braces I think) making it hard for anyone to recreate.

1
votes

On the MyCredentials.java file the name of the @XmlRootElement(name = "Credentials") starts with a capital C and in the @WebParam(name = "credentials") it does not. Maybe its because of this?