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

  • Call a webservice

    Hi all!!

    I'm quite new in web services and I don't know how call a web service using Spring. I've been reading reference documentation for Spring Web Services: chapter 6, which explains the client side but I'm not able to call a web service.
    I tried this simple code:
    Code:
    private void customSendAndReceive(){
    	final String uri="http://localhost:8080/Fibonacci/services/FibonacciImpl?wsdl";
    	
    	StreamSource source = new StreamSource(new StringReader("10"));
    	StreamResult result = new StreamResult(System.out);
    	final WebServiceTemplate wst = new WebServiceTemplate();
    	wst.sendSourceAndReceiveToResult(uri, source, result);
    }
    But it does not work.
    I already published a web service which was successfully tested using axis in this way:
    Code:
    try {
    	String endPoint = "http://localhost:8080/Fibonacci/services/FibonacciImpl?wsdl";
    
    	Service  service = new Service();
    	Call call = (Call) service.createCall();
    
    	call.setTargetEndpointAddress(endPoint);
    	call.setOperationName("calculateFibonacci");
    	call.addParameter("num", XMLType.XSD_INT, ParameterMode.IN);
    			
    	call.setReturnType(XMLType.XSD_INT);
    			
    	Integer ret = (Integer) call.invoke(new Object[]{i1});
    
    	System.out.println("Result: " + ret);
    }
    I have several questions to call a web service using Spring:
    • How do I tell which is the operation I want to call? (in axis, I use function setOperationName)
    • How do I tell which is the argument type in request and response. (Again... in axis I use addParameter and setReturnType functions)

    Thanks a lot in advance!!
    Last edited by mamntc02; Sep 15th, 2009, 02:48 AM.

  • #2
    No one can help me?
    I think it should be an easy question, and I'm quite desperate because I cannot find an answer: How do I ask for a certain operation in a web service using Spring?

    Thanks.

    Comment


    • #3
      Sample Command Line Client

      Here is a sample command line client:

      application-context.xml

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" 
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
         			http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
          
          <bean id="searchServiceClient" class="com.abccompany.abc.ws.client.SearchServiceClient">
              <property name="defaultUri" value="http://localhost:8080/abc/webservices"/>
              <property name="request" value="classpath:com/abccompany/abc/ws/client/keywordSearchRequest.xml"/>
          </bean>
      
      </beans>
      Command line client:

      Code:
      import java.io.IOException;
      
      import javax.xml.transform.Source;
      
      import org.springframework.core.io.Resource;
      import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
      
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      import org.springframework.xml.transform.ResourceSource;
      import org.springframework.xml.transform.StringResult;
      
      public class SearchServiceClient extends WebServiceGatewaySupport {
          private Resource request;
      
          public void setRequest(Resource request) {
              this.request = request;
          }
      
          public static void main(String[] args) throws IOException {
              ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml", SearchServiceClient.class);
              SearchServiceClient searchClient = (SearchServiceClient) applicationContext.getBean("searchServiceClient");
      
      	Source requestSource = new ResourceSource(request);
              StringResult result = new StringResult();
              getWebServiceTemplate().sendSourceAndReceiveToResult(requestSource, result);
              System.out.println(result);
          }
      }
      keywordSearchRequest.xml

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <searchRequest xmlns="http://www.abccompany.com/ws/schemas">
        <keyword>Economy</keyword>
      </searchRequest>

      Comment


      • #4
        Thanks a lot for your example hoffmandirt.
        However I still don't see which is the operation in web service that you request for.
        Correct me, if I'm wrong because there are some knowledge in web services I don't know, but what I understand in your sample is:
        There's a web service published at http://localhost:8080/abc/webservices, right?
        As far as I know, Web services use to have several operations (or methods), right?

        So, which is the operation you request for in your sample?

        In my sample, WSDL looks like this:
        Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <wsdl:definitions targetNamespace="http://fibonacci" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://fibonacci" xmlns:intf="http://fibonacci" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <!--WSDL created by Apache Axis version: 1.4
        Built on Apr 22, 2006 (06:55:48 PDT)-->
         <wsdl:types>
          <schema elementFormDefault="qualified" targetNamespace="http://fibonacci" xmlns="http://www.w3.org/2001/XMLSchema">
           <element name="calculateFibonacci">
            <complexType>
             <sequence>
              <element name="num" type="xsd:int"/>
             </sequence>
            </complexType>
           </element>
           <element name="calculateFibonacciResponse">
            <complexType>
             <sequence>
              <element name="calculateFibonacciReturn" type="xsd:int"/>
             </sequence>
            </complexType>
           </element>
           <element name="calculateFibonacciRange">
            <complexType>
             <sequence>
              <element name="start" type="xsd:int"/>
              <element name="stop" type="xsd:int"/>
             </sequence>
            </complexType>
           </element>
           <element name="calculateFibonacciRangeResponse">
            <complexType>
             <sequence>
              <element maxOccurs="unbounded" name="calculateFibonacciRangeReturn" type="xsd:int"/>
             </sequence>
            </complexType>
           </element>
          </schema>
         </wsdl:types>
        
           <wsdl:message name="calculateFibonacciRangeRequest">
        
              <wsdl:part element="impl:calculateFibonacciRange" name="parameters"/>
        
           </wsdl:message>
        
           <wsdl:message name="calculateFibonacciRequest">
        
              <wsdl:part element="impl:calculateFibonacci" name="parameters"/>
        
           </wsdl:message>
        
           <wsdl:message name="calculateFibonacciResponse">
        
              <wsdl:part element="impl:calculateFibonacciResponse" name="parameters"/>
        
           </wsdl:message>
        
           <wsdl:message name="calculateFibonacciRangeResponse">
        
              <wsdl:part element="impl:calculateFibonacciRangeResponse" name="parameters"/>
        
           </wsdl:message>
        
           <wsdl:portType name="FibonacciImpl">
        
              <wsdl:operation name="calculateFibonacci">
        
                 <wsdl:input message="impl:calculateFibonacciRequest" name="calculateFibonacciRequest"/>
        
                 <wsdl:output message="impl:calculateFibonacciResponse" name="calculateFibonacciResponse"/>
        
              </wsdl:operation>
        
              <wsdl:operation name="calculateFibonacciRange">
        
                 <wsdl:input message="impl:calculateFibonacciRangeRequest" name="calculateFibonacciRangeRequest"/>
        
                 <wsdl:output message="impl:calculateFibonacciRangeResponse" name="calculateFibonacciRangeResponse"/>
        
              </wsdl:operation>
        
           </wsdl:portType>
        
           <wsdl:binding name="FibonacciImplSoapBinding" type="impl:FibonacciImpl">
        
              <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        
              <wsdl:operation name="calculateFibonacci">
        
                 <wsdlsoap:operation soapAction=""/>
        
                 <wsdl:input name="calculateFibonacciRequest">
        
                    <wsdlsoap:body use="literal"/>
        
                 </wsdl:input>
        
                 <wsdl:output name="calculateFibonacciResponse">
        
                    <wsdlsoap:body use="literal"/>
        
                 </wsdl:output>
        
              </wsdl:operation>
        
              <wsdl:operation name="calculateFibonacciRange">
        
                 <wsdlsoap:operation soapAction=""/>
        
                 <wsdl:input name="calculateFibonacciRangeRequest">
        
                    <wsdlsoap:body use="literal"/>
        
                 </wsdl:input>
        
                 <wsdl:output name="calculateFibonacciRangeResponse">
        
                    <wsdlsoap:body use="literal"/>
        
                 </wsdl:output>
        
              </wsdl:operation>
        
           </wsdl:binding>
        
           <wsdl:service name="FibonacciImplService">
        
              <wsdl:port binding="impl:FibonacciImplSoapBinding" name="FibonacciImpl">
        
                 <wsdlsoap:address location="http://localhost:8080/Fibonacci/services/FibonacciImpl"/>
        
              </wsdl:port>
        
           </wsdl:service>
        
        </wsdl:definitions>
        You can see there are two operation (or methods), one called calculateFibonacci, and the other called calculateFibonacciRange both in http://localhost:8080/Fibonacci/services/FibonacciImpl.
        You understand what I mean?

        Thanks.

        Comment


        • #5
          I'm using the PayloadRootQNameEndpointMapping to map my web services to endpoints on the server. So in the example above, the root element of my XML request, which is searchRequest, will be parsed out and mapped to the endpoint configured below.

          Code:
          <bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
          	<property name="mappings">
          		<props>
          			<prop key="{http://www.abccompany.com/ws/schemas}searchRequest">searchEndpoint</prop>
          		</props>
          	</property>
          </bean>

          Comment


          • #6
            Nice, thanks a lot.

            Comment

            Working...
            X