Making a web service request parameter a required field
Asked Answered
S

1

6

The is code first approach to Jax-WS web service.

@WebService (serviceName = "MyInstallPhotoService")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use=SOAPBinding.Use.LITERAL, parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class MyInstallPhotoWS {

    private MyInstallPhotoManager myInstallPhotoManager;

    @Resource
    WebServiceContext context;


    @WebMethod(operationName = "getMyInstallPhoto")
    @WebResult(name = "PhotoRetrievalResponse", partName = "PhotoRetrievalResponse")
    public MyInstallPhotoResponse getBadgePhoto(@WebParam(name = "BadgeNumber", partName = "BadgeNumber") String badgeNumber, @WebParam(name = "LastName", partName = "LastName") String lastName) {
        MyInstallPhotoResponse myInstallPhotoResponse = new MyInstallPhotoResponse();
        try {
            // more code here
        } catch (Exception e) {
          e.printStackTrace();
        }
        return myInstallPhotoResponse;
     }
}

In the above code MyInstallPhotoResponse is defined in a xml schema. The SoapUI request generated something like this

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <rsw:getBadgePhoto>
         <!--Optional:-->
         <rsw:BadgeNumber>I180748-003</rsw:BadgeNumber>
         <!--Optional:-->
         <rsw:LastName>Jones</rsw:LastName>
      </rsw:getBadgePhoto>
   </soapenv:Body>
</soapenv:Envelope>

How can make the BadgeNumber and LastName a required field as opposed to optional as per the soapui request. I tried to move the badgeNumber and lastName to a object myinstallphotorequest (defined in schema) and made the two parameters requried. this the soapui request I got.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myin="http://www.lexisnexis.com/myInstallPhotoService" xmlns:myin1="http://www.lexisnexis.com/schema/myInstallPhotoServiceTypes">
   <soapenv:Header/>
   <soapenv:Body>
      <myin:getMyInstallPhoto>
         <!--Optional:-->
         <myin:MyInstallPhotoRequest>
            <myin1:badgeNumber>?</myin1:badgeNumber>
            <myin1:lastName>?</myin1:lastName>
         </myin:MyInstallPhotoRequest>
      </myin:getMyInstallPhoto>
   </soapenv:Body>
</soapenv:Envelope>

Again I was not able to remove the Optional for the parameter "MyInstallPhotoRequest".

Stillwell answered 25/2, 2013 at 16:36 Comment(0)
T
11

If you check the WSDL file for your web service, the parameter should have minOccurs=0. That's why the SOAPUI request put the optional comments there.

Please use @XmlElement(required=true) to annotate your WebParam that is required.

Tilsit answered 25/2, 2013 at 17:18 Comment(3)
Yes the WSDL shows it as minOccures=0. Also I'm not able to add @XmlElement(required=true) to the web param it says "The annotation XmlElement is disallowed for this location". I did this public MyInstallPhotoResponse getBadgePhoto(@XmlElement(required=true) @WebPa.... Is this right?Stillwell
Please read the thread: #8211920.Tilsit
BTW, I would strongly recommend "contract first" approach rather than "code first" approach.Tilsit

© 2022 - 2024 — McMap. All rights reserved.