8 Replies Latest reply on Nov 18, 2012 4:37 PM by spyhunter99

    Consume a web service from another web service

    florentine Newbie

      Hi all,

      I'm new to the web service world so I apologize for my naive question.

       

      I created a very basic web service that I am able to successfully call from a web client. What I want to do now is to create a second web service that uses the service exposed by the first one to provide its own functionality.

       

      These are my classes/web services:

       

       

      HelloWorld.java (External Web Service)

      {code}

      package hw1;

       

      import javax.jws.WebService;

      import javax.jws.WebMethod;

      import javax.jws.soap.SOAPBinding;

      import javax.jws.soap.SOAPBinding.Style;

       

      @WebService

      @SOAPBinding(style = Style.RPC)

      public interface HelloWorld {

       

          @WebMethod String getHiWorld();

      }

      {code}

       

       

       

      HelloWorldImpl.java

      {code}

      package hw1;

       

      import hw2.HelloWorld2;

       

       

      import javax.jws.WebMethod;

      import javax.jws.WebService;

      import javax.xml.ws.WebServiceRef;

       

      @WebService(serviceName = "HelloWorldService", name = "HelloWorld")

      public class HelloWorldImpl implements HelloWorld {

       

          @WebServiceRef(type = HelloWorld2.class)

            private HelloWorld2Service service;

          @Override

          @WebMethod

          public String getHiWorld() {

      HelloWorld2 port = service.getHelloWorld2Port();

        

              return "Hi World" + port.getHiWorld();

       

          }

       

      }

      {code}

       

      HelloWorld2.java (Internal Web Service)


      {code}

      package hw2;

       

      import javax.jws.WebService;

      import javax.jws.WebMethod;

      import javax.jws.soap.SOAPBinding;

       

       

      @WebService

      @SOAPBinding(style=SOAPBinding.Style.RPC)

      public interface HelloWorld2 {

       

          @WebMethod String getHiWorld();

      }

      {code}

       

       

      HelloWorld2Impl.java

      {code}

      package hw2;

       

      import javax.jws.WebMethod;

      import javax.jws.WebService;

       

       

       

      @WebService(serviceName="HelloWorld2Service", name="HelloWorld2")   

      public class HelloWorld2Impl implements HelloWorld2 {

       

       

          @Override

          @WebMethod

          public String getHiWorld() {

                  return " again!";

       

       

          }

       

      public HelloWorld2Impl (){}

      }

      {code}

       

      The error I get is that in HelloWorld.java, the HelloWorld2Service cannot be resolved to a type.

      I am developing with Eclipse Indigo, I'm using JBOSS 7 AS, and I have the Java 1.7 installed on my pc.

       

      Thanks in advance for any help or hint you can provide.

       

      Filippo

        • 1. Re: Consume a web service from another web service
          spyhunter99 Novice

          why not just call it via transport instead of by reference?

          • 2. Re: Consume a web service from another web service
            spyhunter99 Novice

            you can also do a jdni lookup to get the reference

            • 3. Re: Consume a web service from another web service
              florentine Newbie

              Thank you for you answer. I took a look at JNDI and it looks a lower level solution where you have to code more. I saw several examples on the internet on how to create a client for a web service and I can make them work also on my small piece of code above. The problem is that I can't make a web service calling another web service. It sounds to me a very basic functionality, but I was really able to find a complete example on how to do that. I tried to copy and paste the code of my client inside the HelloWorld.getHiWorld() method, but I keep getting a proxy error. How people normally make more webservices communicating between each other? any example from where I could get an idea?

               

              This is my updated code with the client :

               

               

              HelloWorldImpl.java

              package hw1;

               

              import hw2.HelloWorld2;

               

              import java.net.MalformedURLException;

              import java.net.URL;

              import javax.jws.WebService;

              import javax.xml.namespace.QName;

              import javax.xml.ws.Service;

               

               

              @WebService(serviceName="HelloWorldService", name="HelloWorld")

              public class HelloWorldImpl implements HelloWorld   {

               

                  @Override

                  public String getHiWorld() {

                     

                      URL url=null;

                      try {

                          url = new URL("http://localhost:8080/HelloWorld2WS/HelloWorld2Service?wsdl");

                      } catch (MalformedURLException e) {

                          // TODO Auto-generated catch block

                          e.printStackTrace();

                      }

                      QName qname = new QName("http://hw2/", "HelloWorld2Service");

                      Service    service = Service.create(url, qname);

                      HelloWorld2 port = service.getPort(HelloWorld2.class);

                     

                      return "Hi World" + port.getHiWorld();

                  }

               

                  public HelloWorldImpl() {

               

                  }

               

              }

               

               

               

              HelloWorld2Impl.java

               

              package hw2;

               

              import javax.jws.WebService;

               

              @WebService(serviceName="HelloWorld2Service", name="HelloWorld2")

              public class HelloWorld2Impl implements HelloWorld2 {

               

                  @Override

                  public String getHiWorld() {

                          return " again!";

                   }

                 

              public HelloWorld2Impl (){}

              }

               

               

               

              HelloWorldClient.java

               

              package client;

               

              import hw1.HelloWorld;

               

              import java.net.URL;

               

              import javax.xml.namespace.QName;

              import javax.xml.ws.Service;

               

               

               

              public class HelloWorldClient {

                  public static void main(String[] args) throws Exception {

                      URL url = new URL("http://localhost:8080/HelloWorldWS/HelloWorldService?wsdl");

                      QName qname = new QName("http://hw1/", "HelloWorldService");

               

                      Service service = Service.create(url, qname);

               

                      HelloWorld eif = service.getPort(HelloWorld.class);

               

                      System.out.println(eif.getHiWorld());

               

                  }

               

              }

               

              Finally, this is the error my JBoss server is giving me:

               

               

              Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: hw2/HelloWorld2

                  at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:178)

                  at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:111)

                  at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)

                  at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)

                  at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:129)

                  at $Proxy19.getHiWorld(Unknown Source)

                  at client.HelloWorldClient.main(HelloWorldClient.java:25)

               

               

              Thank you!

              • 4. Re: Consume a web service from another web service
                spyhunter99 Novice

                I do think sort of thing all the time, but always from wsdl first.

                 

                What version of the JDK are you using, and Jboss, and JbossWS.

                 

                I never use the Service API either. I always use the generated (from wsdl first) class that is annotated with @WebServiceClient

                • 5. Re: Consume a web service from another web service
                  florentine Newbie

                  I'm using

                  • JBoss AS 7.1
                  • Java 1.7.0_02
                  • jbossws-cxf-client-4.0.1.GA.jar
                  • Eclipse Indigo
                  • 6. Re: Consume a web service from another web service
                    spyhunter99 Novice

                    I've made a working sample for you, wsdl first. source included. It's a netbeans project, but should be adaptable to eclipse

                    • 7. Re: Consume a web service from another web service
                      Genrose Hirudaysamy Newbie

                      I am using Jboss 7.1.1

                       

                      I am unable to run the attached hello service successfully.

                      It gives the following exception:

                       

                      15:20:50,458 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/HelloWorldJAXWS].[jsp]] (http--127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: javax.xml.ws.WebServiceException: Port {http://www.examples.com/wsdl/HelloService}Hello_Port not found.
                              at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:328)
                              at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:319)
                              at javax.xml.ws.Service.getPort(Service.java:99) [jboss-jaxws-api_2.2_spec-2.0.0.Final.jar:2.0.0.Final]
                              at com.examples.wsdl.helloservice.HelloServiceClient.getHelloPort(HelloServiceClient.java:57) [classes:]

                       

                      Could you show light on this. How to deploy hello.wsdl?

                      • 8. Re: Consume a web service from another web service
                        spyhunter99 Novice

                        I'm assuming that it deployed correctly. The most likely cause is in the HelloServiceClient.java, try changing the Hello_Port annotation to  Hello_PortType

                         

                         

                        I wrote the example using jbossws native on jbossas 5 and 6.