How to define a default value for a @WebParam in a java web service?

15,060

What you're after cannot be implemented with your current webservice style. First, JAXB will not inject the default value into the message, if the element is not present in the original message sent. From the JAXB manual

element defaults do not kick in when the element is absent, so unfortunately we can't change this behavior.

As a workaround, you could change your webservice style to WRAPPED. With this style, you can encapsulate your webservice parameters in a single parent type (a simple RequestWrapper if you will). Given that flexibility, your clients can supply a single parent type that may or may not contain both parameters. Your wrapper should look something like:

@XmlRootElement
public class YourWrapper {

 String company;

 @XmlElement(name="limitTop",required="false",defaultValue="100")
 int limitTop; 

//getters and setters
}

Also,

@RequestWrapper(targetNamespace="http://you.com/ws/types", className="com.you.YourWrapper") 
@WebMethod(operationName = "getCustomers")
@WebResult(name = "customer")
public Customer[] getCustomers(@WebParam(name = "company") String company,
       @WebParam(name = "limitTop") int limitTop){

       //Logic of getting customer be here

}

Given the above, JAXB will be able to ignore the absence of the value and possibly set the default after the unmarshalling

Share:
15,060
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    i have been trying to find a solution for this particular topic:

    It is possible to define a default value in a @webParam annotation for a SOAP/WSDL based Web Service? What i want is exactly the following result:

    I Have a @WebMethod in my web service called getCustomers with the following signature:

    @WebMethod(operationName = "getCustomers")
    @WebResult(name = "customer")
    public Customer[] getCustomers(@WebParam(name = "company") String company,
           @WebParam(name = "limitTop") int limitTop){
    
           //Logic of getting customer be here
    
    }
    

    Particularly, the parameter limitTop is not optional in this method, this number indicates how many customer records you want the response returns, but i want that in my xml schema, this particular element has a default value, something like this:

    <xs:complexType name="getCustomers">
        <xs:sequence>
            <xs:element name="company" type="xs:string" minOccurs="0"/>
            **<xs:element name="limitTop" type="xs:int" default="100"/>**
        </xs:sequence>
    </xs:complexType>
    

    Please notice the default attribute in the element named limitTop

    This is in order to allow to the client application send a SOAP message without the limitTop element, but still limit the number of records returned to the default value given in the limitTop element.

    I am using Java, JAX-WS, and GlassFish Server Open Source Edition.

    Thanks for your help.

    Carlo Yañez

  • Admin
    Admin almost 10 years
    Ok, kolossus, Thank you for your help, i have your suggested solution very clear, what i don't have clear at all is the mentioned webservice style change to WRAPPED, what is supposed i should do to change my webservice style? Anyway, your answer help me a lot to clarify my doubts about this issue. Thank you again.
  • kolossus
    kolossus almost 10 years
    @CarloYañez - take a look at this answer
  • Admin
    Admin almost 10 years
    Thanks a lot Kolossus.