2 Replies Latest reply on Jun 17, 2008 3:42 AM by sagi mann

    Web Service Client

    Ben Sisson Newbie

      I have found several very good tutorials for creating a web service using annotations (JSR-181). I am struggling however creating a web service client using annotations. I'm working with Eclipse 3.3 and Seam 2.0. I did the old web service client way of creating a project, then generating a web service client from the wsdl which generates the stubs and this works fine. However, I want to be able to do this with annotations instead and am struggling. I have seen examples that use the @WebServiceRefs annotations but no complete explenation. I would expect if I use annotations that I will not have to generate the stubs from the WSDL but have no clarity. If you can point me to a good tutorial that would be great? If you could clarify whether I need to generate the stubs from the WSDL that would be helpful as well?

      Thanks.

        • 1. Re: Web Service Client
          Ben Sisson Newbie

          In my Eclipse client I generated the web service stubs and proxy by creating a new project, then right clicking on the project selecting new->other->web service->web service client. When setting up the client I pointed it to my web service WSDL file ex:(http://localhost:8280/PSDynamicWebProject/TestWs?wsdl). I then just setup a simple test class to call the web service (code below):

          package service.client;

          import java.rmi.RemoteException;

          import org.domain.PSDynamicWebProject.pojo.TestWs;
          import org.domain.PSDynamicWebProject.pojo.TestWsProxy;

          public class RunnerOld
          {
          public static void main(String[] args)
          {
          TestWs tw = new TestWsProxy();
          try
          {
          System.out.println(tw.greet("1"));
          }
          catch (RemoteException e)
          {
          System.out.println(e.getMessage());
          }
          }
          }

          This code worked fine (called the web service and received the correct response back).

          Now I wanted to create the web service client using the annotations from JSR-181. I looked a several different examples and the best that I can conclude is that I still need to generate the stubs and proxy files so I set them up in my project in the same way as I did above. I then create a simple class to call my web service (code below):

          package service.client;

          import java.rmi.RemoteException;
          import javax.xml.rpc.ServiceException;
          import javax.xml.ws.WebServiceRef;
          import org.domain.PSDynamicWebProject.pojo.TestWs;
          import org.domain.PSDynamicWebProject.pojo.TestWsService;

          public class Runner
          {
          @WebServiceRef(wsdlLocation="http://127.0.0.1:8280/PSDynamicWebProject/TestWs?wsdl")
          private static TestWsService service;

          public static void main(String[] args)
          {
          try
          {
          TestWs tw = service.getTestWsPort();
          System.out.println(tw.greet("1"));
          }
          catch (RemoteException e)
          {
          System.out.println(e.getMessage());
          }
          catch (ServiceException e)
          {
          System.out.println(e.getMessage());
          }
          }
          }

          This client however failed. It got a java null pointer exception on this statement: TestWs tw = service.getTestWsPort(); I debugged it and found tha the service object was null.

          I have several questions regarding this approach:

          1) I am curious why the service object was null and I got a null pointer exception?

          2) Is this the correct way to setup and use the JSR-181? I find it interesting that if we still must generate the stubs and prox from the WSDL before we can use the annotations then what advantage is there to using the annotations?

          Any clarity and help would be greatly appreciated. I can see the benefit in using JSR-181 to setup the web services from previous web services that I setup and the advantage of the annotations. However, I don't see any benefits in the annotations on the client if I'm doing this correctly (which I may not be).

          Here is the simple code from my web serivce for review (this works):

          package org.domain.PSDynamicWebClient.pojo;

          import java.io.StringWriter;
          import java.util.Date;

          import javax.jws.WebMethod;
          import javax.jws.WebParam;
          import javax.jws.WebService;
          import javax.jws.soap.SOAPBinding;
          import javax.xml.bind.JAXBContext;
          import javax.xml.bind.Marshaller;
          import javax.xml.transform.stream.StreamResult;

          import org.domain.PSDynamicWebClient.entity.Employee;
          import org.domain.PSDynamicWebClient.session.SelectEmployee;
          import org.jboss.seam.Component;


          /**
          * This is a webservice class exposing a method called greet which takes a
          * input parameter and greets the parameter with hello.
          *
          * @author dhanago
          */

          /*
          * @WebService indicates that this is webservice interface and the name
          * indicates the webservice name.
          */
          @WebService(name = "TestWs", serviceName = "TestWsService")
          /*
          * @SOAPBinding indicates binding information of soap messages. Here we have
          * document-literal style of webservice and the parameter style is wrapped.
          */
          @SOAPBinding
          (
          style = SOAPBinding.Style.DOCUMENT,
          use = SOAPBinding.Use.LITERAL,
          parameterStyle = SOAPBinding.ParameterStyle.WRAPPED
          )
          public class TestWs
          {
          @WebMethod
          public String greet( @WebParam(name = "emplid")
          String emplid )
          {
          try
          {
          /**************************************************************************
          * The following example marshals an object class into an XML string which
          * is returned to the caller. The object is an entity representing a
          * row on the database.
          **************************************************************************/
          // Query the database for an employee's information
          SelectEmployee selectEmployee = (SelectEmployee) Component.getInstance("selectEmployee");
          selectEmployee.setId(emplid);
          Employee e = selectEmployee.getInstance();

          // Marshal the employee information into an xml string and return it
          JAXBContext context = JAXBContext.newInstance(Employee.class);
          Marshaller marshaller = context.createMarshaller();
          StreamResult result = new StreamResult( new StringWriter());
          marshaller.marshal(e, result);
          return result.getWriter().toString();
          }
          catch (Exception e)
          {
          return e.getMessage();
          }
          }
          }

          • 2. Re: Web Service Client
            sagi mann Newbie

            hi, I'm having the same problem with the NPE. Did you solve this? By the way, I noticed that the latest JBossWS comes with many test cases, some of which uses annotations and seem to pass the test-run... However, I was still unable to "reproduce" the success...