Announcement Announcement Module
Collapse
No announcement yet.
Access external webservice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Access external webservice

    Hi.
    In my facade, I want to access a web service deployed on another web application.
    Can I use Spring in my facade to access that web service ?
    I read Spring's docs and it looks that I can use Spring only if the web service is deployed in the same web app.
    Can I use Spring to access web services on another deployed application ?
    Thanks for help.

  • #2
    Hello,

    sure :-)

    There are basically lots of possible ways. Here are a few ones.

    1.) JAX-RPC: You have to use an JAX-RPC compliant webservice engine (axis 1.X) and call the webservice. With Jax-RPC you can make dynamic calls or generate static stubs and use them to call the webservice. Have a look in the reference documentation, also the sample application jpetstore has a sample on that.

    2.) XFire: You can use the XFire Webservice engine, they have some built-in spring support (predefined beans that can be used to build you service). There you have also the option dynamic call or generated stubs.

    3.) Spring WS: Actually there is no final release avaiable, but an rc1 which is very stable except for one bug that should not affect you in the client side. Have a look at the webseite or forum for more informations.

    Regards
    agim

    Comment


    • #3
      Here is a snippet from Spring's doc :
      Code:
          <bean id="accountWebService" class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean">
              <property name="serviceInterface" value="example.RemoteAccountService"/>
              <property name="wsdlDocumentUrl" value="http://localhost:8080/account/services/accountService?WSDL"/>
              <property name="namespaceUri" value="http://localhost:8080/account/services/accountService"/>
              <property name="serviceName" value="AccountService"/>
              <property name="portName" value="AccountPort"/>
          </bean>
      I want to access another deployed web service, how could I know the value of serviceInterface property ?
      The web service could not even written in Java.

      Comment


      • #4
        Hi,

        you could get the service interface by the service provider, or just generate it with an jax-rpc compliant ws-engine (axis)

        regards
        agim

        Comment


        • #5
          Could some provide code example, showing how to consome an external web service made with spring and Xfire.
          I have Unit test to test my web service. And every thing is OK.

          Complete Working version of my web service code:

          My xfire-servlet.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>

          <bean id="webAnnotations"
          class="org.codehaus.xfire.annotations.jsr181.Jsr18 1WebAnnotations" />

          <bean id="handlerMapping"
          class="org.codehaus.xfire.spring.remoting.Jsr181Ha ndlerMapping">
          <property name="typeMappingRegistry">
          <ref bean="xfire.typeMappingRegistry" />
          </property>
          <property name="xfire">
          <ref bean="xfire" />
          </property>
          <property name="webAnnotations">
          <ref bean="webAnnotations" />
          </property>
          </bean>

          <bean
          class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
          <property name="urlMap">
          <map>
          <entry key="/">
          <ref bean="handlerMapping" />
          </entry>
          </map>
          </property>
          </bean>

          <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />

          </beans>

          My spring applicationContext.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

          <bean id="netSendService"
          class="com.ws.NetSendServiceImpl" />
          </beans>

          My web.xml:
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

          <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>
          /WEB-INF/classes/applicationContext.xml
          /WEB-INF/classes/xfire-servlet.xml
          </param-value>
          </context-param>

          <listener>
          <listener-class>
          org.springframework.web.context.ContextLoaderListe ner
          </listener-class>
          </listener>

          <servlet>
          <servlet-name>XFireServlet</servlet-name>
          <servlet-class>
          org.codehaus.xfire.spring.XFireSpringServlet
          </servlet-class>
          </servlet>

          <servlet-mapping>
          <servlet-name>XFireServlet</servlet-name>
          <url-pattern>/servlet/XFireServlet/*</url-pattern>
          </servlet-mapping>

          <servlet-mapping>
          <servlet-name>XFireServlet</servlet-name>
          <url-pattern>/services/*</url-pattern>
          </servlet-mapping>

          </web-app>

          My Interface and Implementation:
          /**
          *
          */
          package com.ws;

          import java.io.IOException;
          import java.util.List;

          import javax.jws.WebService;

          /**
          * @author marcos.sousa
          * @since 1.1
          * @see NetSendServiceImpl
          *
          * http://localhost:8080/Alert/services...ndService?wsdl
          *
          */
          @WebService
          public interface NetSendService {

          public void sendNetSend(String workstation, String mensagem) throws IOException;

          }

          /**
          *
          */
          package com.ws;

          import java.io.IOException;
          import java.util.List;

          import javax.jws.WebService;

          /**
          * @author marcos.sousa
          *
          */
          @WebService(serviceName = "NetSendService", endpointInterface = "com.ws.NetSendService")
          public class NetSendServiceImpl implements NetSendService {

          public void sendNetSend(String workstation, String mensagem) throws IOException {
          Runtime.getRuntime().exec("net send /domain:" + workstation + " " + mensagem);
          }

          }

          My Unit test:

          /**
          *
          */
          package com.teste;

          import java.io.IOException;
          import java.net.MalformedURLException;
          import java.util.ArrayList;
          import java.util.List;

          import junit.framework.TestCase;

          import org.codehaus.xfire.annotations.AnnotationServiceFa ctory;
          import org.codehaus.xfire.client.XFireProxyFactory;
          import org.codehaus.xfire.service.Service;

          import com.ws.NetSendService;
          import com.ws.NetSendServiceImpl;

          /**
          * @author marcos.sousa
          *
          */
          public class TestNetSendService extends TestCase {

          private static String URL_NETSEND_SERVICE = "http://localhost:8080/Alert/services/NetSendService";

          private NetSendService netSendService;

          private static String WORKSTATION = "w27";

          /**
          * @param arg0
          */
          public TestNetSendService(String arg0) {
          super(arg0);
          }

          /*
          * (non-Javadoc)
          *
          * @see junit.framework.TestCase#setUp()
          */
          protected void setUp() throws Exception, MalformedURLException {
          super.setUp();
          Service service = new AnnotationServiceFactory().create(NetSendServiceIm pl.class);
          netSendService = (NetSendService) new XFireProxyFactory().create(service, URL_NETSEND_SERVICE);
          }

          /*
          * (non-Javadoc)
          *
          * @see junit.framework.TestCase#tearDown()
          */
          protected void tearDown() throws Exception {
          super.tearDown();
          }

          /**
          * Test method for {@link com.bci.bcialerta.ws.NetSendServiceImpl#sendNetSen d(java.lang.String, java.lang.String)}.
          */
          public final void testSendNetSend() throws IOException {
          netSendService.sendNetSend(WORKSTATION, "testSendNetSend");
          }

          }

          As you see, until here every thing is working very well.

          So could some post code that consume this web service in an external project?

          Thanks in advance.

          Regards,

          Marcos de Sousa
          Last edited by sousa1981; Jul 3rd, 2007, 09:14 AM. Reason: More talk...

          Comment

          Working...
          X