Skip to main content

Xml element name in SOAP response for the class returned by WebMethod ?

1 reply [Last post]
tomjo
Offline
Joined: 2008-05-17
Points: 0

How can you define the name of the xml element in the SOAP response for an exposed class that is returned from a JAX-WS WebMethod ?

I have tried the JAXB annotations @XmlRootElement and @XmlType, as illustrated further down, without success.

It is the below xml name element "return" that is annoying me and I would like to be able to rename it (with some annotation, I guess should be possible ?):

<?xml version="1.0" encoding="UTF-8"?>

9
...

These are the annotations I have been experimenting with, which produced the above result (with Netbeans and Glassfish):

@WebMethod(operationName = "getPerson1")
public Person getPerson(@WebParam(name = "id") int id) {
...

@XmlRootElement(name="person3")
@XmlType(name="person2")
public class Person {

@XmlElement(name="Identity")
public int getId() {
return id;
}
...

When I have deployed the above webmethod and look at the schema (referred from the WSDL) i.e. the URL
http://localhost:8080/...Service?xsd=1
the following part from the schema defines a "return" name that I wish would have been "person2" or "person3" (I am using these different suffixes in the annotations, just to trying to see what annotation that ends up where in the schema...):
...

...

/ Tom

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tomjo
Offline
Joined: 2008-05-17
Points: 0

I have found the answer to my question.
Maybe someone else will find this question through some search engine, so here is the answer:

Annotate the WebMethod with the annotation @WebResult, where the property "name" will define the name of the xml element corresponding to the class that is exposed as return value from the method, i.e. something like this:
@WebMethod(operationName = "getPerson1")
@WebResult(name="person")
public Person getPerson(@WebParam(name = "id") int id) {
...
should result in a SOAP response like this:



9
...

Information source (JAX-WS 2.1 specification) :
http://jcp.org/aboutJava/communityprocess/mrel/jsr224/index2.html
> 7.11.5 javax.jws.WebResult
> @Target({METHOD})
> public @interface WebResult {
> String name() default "return";
> ...

/ Tom