Change bean representation in SOAP Service (JAX-WS/METRO)

610 Views Asked by At

I have this class Color:

public class Color {

private String name;
private List<String> usedInShapes;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<String> getUsedInShapes() {
    if (usedInShapes== null) {
        return null;
    }
    return new ArrayList<String>(usedInShapes);
}
}

And is returned this bean is returned on a wrapper that has a list of Color object

public class ColorListResponse {

    private final List<Color > colorList;

    @XmlElement(required=true)
    public List<Color> getColorList() {
        return colorList;
    }
}

When the method is called the user gets a response like

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getColorsResponse xmlns:ns2="http://example.com/ws">
         <return>
            <colorList>
               <name>YELLOW</name>
            </colorList>
            <colorList>
               <name>BLUE</name>
            </colorList>
            <colorList>
               <name>RED</name>
            </colorList>
            <colorList>
               <name>BROWN</name>
            </colorList>
         </return>
      </ns2:getColorsResponse>
   </S:Body>
</S:Envelope>

What i need is something like:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getColorsResponse xmlns:ns2="http://example.com/ws">
         <return>
            <colorList>YELLOW</colorList>
            <colorList>BLUE</colorList>
            <colorList>RED</colorList>
            <colorList>BROWN</colorList>
         </return>
      </ns2:getColorsResponse>
   </S:Body>
</S:Envelope>

I've been trying messing around with the annotations but with no success...

1

There are 1 best solutions below

2
On

You can't make the tool writes that kind of response. To accomplish it, you have to write your own implementation to marshal/unmarshal the XML. By default, the tool is generating an XML using the class type and the attributes inside it.

public class Color {
     private String name;
}

Also, your method returns List<Vendor> not List<Color>.