How to name xs: elements in generated wsdl?

I recently launched my WebServicesExplorer in my Eclipse, and I just realized that the xs:element names generated by JAXB are not so verbose. Here is one of the sequences:

 <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string" /> <xs:element minOccurs="0" name="arg1" type="xs:string" /> <xs:element name="arg2" type="xs:int" /> </xs:sequence> 

generated from this file:

 import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class User { private String firstName; private String lastName; private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 

I'm not a JAXB expert yet, so I was wondering if I can change these arg * elements into something reasonable, for example, in the POJO class?

+4
source share
1 answer

You do not indicate what kind of web services structure you use, but the behavior is similar to JAX-WS specifications, for example. The parts in my generated wsdl have the form names "arg0", "arg1", ... Why don't the parts (and Java created from them) use the nice parameter names that I entered in the interface definition?

Formal Answer: The JAX-WS specification (in particular section 3.6.1) requires that it be generated in this way. To customize the name, you must use the @WebParam (name = "blah") annotation to indicate the best names. (You can use @WebResult for the return value, but you will only see results if you look at XML.)

Reason: One of the mysteries of java is that abstract methods (and therefore interface methods) do NOT get parameter names compiled into them, even with debugging information. Thus, when the service model is built from an interface, there is no way to determine the names that were used in the source code.

If the service is built from a specific class (instead of an interface) And the class was compiled with debugging information, we can get the parameter names. A simple interface does this. However, this can cause potential problems. For example, when you move from development to production, you can turn off debugging information (remove -g from the javac flags), and suddenly the application may break, since the generated wsdl (and, therefore, waiting for soap messages) will change. As such, the JAX-WS specs went a safe route and set the mandate that you should use @WebParam annotations to specify more descriptive names.

You can try adding an XmlElement annotation to each field and specify a name, but are you sure this is due to JAXB? If I run the circuit with POJO code, this is what I get (names are matched correctly):

 <xs:complexType name="user"> <xs:sequence> <xs:element name="age" type="xs:int"/> <xs:element name="firstName" type="xs:string" minOccurs="0"/> <xs:element name="lastName" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> 

Based on your comment, if you "forcefully" use POJO for the following code, do you still get duplicate name complaints?

 import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlAccessType; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement public class User { @XmlElement(name = "firstName") private String firstName; @XmlElement(name = "lastName") private String lastName; @XmlElement(name = "age") private int age; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 
+1
source

All Articles