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".

Gary
  • 13,303
  • 18
  • 49
  • 71
Ravi
  • 7,939
  • 14
  • 40
  • 43

1 Answers1

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.

Lan
  • 6,470
  • 3
  • 26
  • 37
  • 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? – Ravi Feb 25 '13 at 17:27
  • 1
    Please read the thread: http://stackoverflow.com/questions/8211420/xmlelement-annotation-dissallowed-with-webparam. – Lan Feb 25 '13 at 18:38
  • BTW, I would strongly recommend "contract first" approach rather than "code first" approach. – Lan Feb 25 '13 at 19:34