2

My task is to write a webservice for an update operation where a list of objects are passed to the method.

@WebMethod(operationName = "updateObjects", action = "urn:preferences")
public boolean updateObjects(List<MyObject> objects){

}

The class MyObject is simple enough.

 @XmlRootElement(name="Object")
 public class MyObject{
    private String item1;
    private String item2;
 }

Now the problem statement. When I look at the SOAP request for this method (that SOAP UI generated for me), the request looks like below :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pref="preferences">
   <soapenv:Header/>
   <soapenv:Body>
      <pref:updateObjects>
         <!--Zero or more repetitions:-->
         <arg0>
            <!--Optional:-->
            <item1>?</item1>
            <!--Optional:-->
            <item2>?</item2>
         </arg0>
      </pref:updateObjects>
   </soapenv:Body>
</soapenv:Envelope>

but I want it to look like below.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pref="preferences">
   <soapenv:Header/>
   <soapenv:Body>
      <pref:updateObjects>
         <!--Zero or more repetitions:-->
         <Objects>
             <Object>
                <!--Optional:-->
                <item1>?</item1>
                <!--Optional:-->
                <item2>?</item2>
             </Object>
             <Object>
                <!--Optional:-->
                <item1>?</item1>
                <!--Optional:-->
                <item2>?</item2>
             </Object>
         </Objects>  
      </pref:updateObjects>
   </soapenv:Body>
</soapenv:Envelope>  

Can somebody please advice. Thanks in advance.

Vinay
  • 405
  • 2
  • 10
  • 17

1 Answers1

5

You only need to add a 'wrapper' to your List of objects like this:

 @XmlRootElement(name="objects")
 public class MyObjects{

    @XmlElement(name="object")
    List<MyObject> myObjects;
 }

public class MyObject{
    private String item1;
    private String item2;
 }

NOTE: changing root element from arg0 to objects with the tag @XmlRootElement(name="objects") will not work because your <objects> is not a root element in the web service definition. Actually it is part of your <wsdl:message> (so JAXB will discart it).

What you need to change is the web service message adding a @WebParam(name = "objects") to your @WebMethod like:

@WebMethod(operationName = "updateObjects", action = "urn:preferences")
public boolean updateObjects(@WebParam(name = "objects") MyObjects objects){

}

If you do not want use a 'wrapper', you can keep your WebMethod but like this:

@WebMethod(operationName = "updateObjects", action = "urn:preferences")
public boolean updateObjects(@WebParam(name = "object") List<MyObject> objects){

}

but you will loose the <objects> wrapper. The request should be something like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pref="preferences">
   <soapenv:Header/>
   <soapenv:Body>
      <pref:updateObjects>
         <!--Zero or more repetitions:-->
         <object>
            <!--Optional:-->
            <item1>?</item1>
            <!--Optional:-->
            <item2>?</item2>
         </object>
      </pref:updateObjects>
   </soapenv:Body>
</soapenv:Envelope>
ggarciao
  • 1,618
  • 16
  • 29
  • Thanks a lot for the quick response ggarcio.. Isn't there a way that we can do this without creating the wrapper class (through any annotation that does this wrapping for us) ? I have 8 such methods for different list of objects :( – Vinay Jun 21 '12 at 08:09