Search code examples
javaweb-servicessoapjax-wswebservice-client

Verifying response call with JAX WS in Java


I have a question about jax ws with Java. In fact i have a client that call a web service method :

Client Implementation :

      URL url = new URL("file:/D:/Projects/Mywsdl.wsdl");
      QName qname = new QName("http://adresse/", "ImpWSService");
      Service service = Service.create(url, qname);
          //web service Interface
      DomaineIntWS domaineIntWS = service.getPort(DomaineIntWS.class);
          //web service methode call   
      String echo = scciProxyIntWS.echocall("xxx");

WS Interface :

         import javax.jws.WebParam;
         import javax.jws.WebService;

         @WebService
         public abstract interface DomaineIntWS
         {
         public abstract String echocall(@WebParam(name="testEcho") String paramString);
         }

My question is how can I add in my client implementation a way that can test the call of ws result for example testing if code response equal 200 OK ?

How can I do that with jax ws?

Thanks guys.


Solution

  • You would need to cast your Port to a BindingProvider, like this:

        URL url = new URL("file:/D:/Projects/Mywsdl.wsdl");
        QName qname = new QName("http://adresse/", "ImpWSService");
        Service service = Service.create(url, qname);
        //web service Interface
        DomaineIntWS domaineIntWS = service.getPort(DomaineIntWS.class);
        try {
        String echo = domaineIntWS.echocall("xxx");
        } catch (Exception e) {
          //do something with a possible exception
        } finally {
    
        BindingProvider bpDomaineIntWS = (BindingProvider) domaineIntWS;
        int httpResponseCode =(Integer) bpDomaineIntWS.getResponseContext().get(MessageContext.HTTP_RESPONSE_CODE);
    
    }
    

    Side note: Have into account that even if you get the value of the response code, the JAX-WS implementation will handle the response.

    In most cases, you shouldn't worry about handling yourself the codes and stuff. That is the whole purpose of JAX-WS. Otherwise you would be using a raw HttpClient library.