2010-03-06 37 views
6

我需要跳轉到Spring Web服務項目,在實現了Spring Web服務的客戶端只..的Spring Web服務客戶端教程或示例中需要

所以,我已經通過與Spring's Client Reference Document了我需要的。

所以,我需要的類客戶端執行的想法。

但我的問題是,像我做了一些google搜索,但並沒有獲得客戶端和服務器的任何適當的例子來自我可以實現我的客戶一個樣本。

所以,如果有人給我從我可以學到我的客戶端實現適當的例子一些鏈接或教程將不勝感激。

在此先感謝...

+0

一個很好的樣本可以在http://stackoverflow.com/questions/18641928/consume-webservice-service-in-找到彈簧-WS-使用-WSDL – 2013-11-24 15:30:03

回答

7
在我以前的項目

,我實現了一個web服務的客戶端與Spring 2.5.6,maven2,這是XMLBeans的。

  • XMLBeans的負責UN /元帥
  • maven2的是項目管理/建築等

我在這裏粘貼一些代碼,並希望他們是有益的。

XMLBeans的Maven插件CONF:(在pom.xml中)

<build> 
     <finalName>projectname</finalName> 

     <resources> 

     <resource> 

      <directory>src/main/resources</directory> 

      <filtering>true</filtering> 

     </resource> 

     <resource> 

      <directory>target/generated-classes/xmlbeans 

      </directory> 

     </resource> 

    </resources> 


     <!-- xmlbeans maven plugin for the client side --> 

     <plugin> 

      <groupId>org.codehaus.mojo</groupId> 

      <artifactId>xmlbeans-maven-plugin</artifactId> 

      <version>2.3.2</version> 

      <executions> 

       <execution> 

        <goals> 

         <goal>xmlbeans</goal> 

        </goals> 

       </execution> 

      </executions> 

      <inherited>true</inherited> 

      <configuration> 

       <schemaDirectory>src/main/resources/</schemaDirectory> 

      </configuration> 

     </plugin> 
<plugin> 

      <groupId>org.codehaus.mojo</groupId> 

      <artifactId>build-helper-maven-plugin 

      </artifactId> 

      <version>1.1</version> 

      <executions> 

       <execution> 

        <id>add-source</id> 

        <phase>generate-sources</phase> 

        <goals> 

         <goal>add-source</goal> 

        </goals> 

        <configuration> 

         <sources> 

          <source> target/generated-sources/xmlbeans</source> 

         </sources> 

        </configuration> 

       </execution> 



      </executions> 

     </plugin> 
    </plugins> 
</build> 

所以從上面的conf,你需要把模式文件(無論是獨立的或在您的WSDL文件,你需要提取它們,保存爲模式文件。)在src/main/resources下。當你用maven構建項目時,pojos將由xmlbeans生成。生成的源代碼將在 target/generated-sources/xmlbeans下。

然後我們來到Spring conf。我只是把WS有關情況在這裏:

<bean id="messageFactory" class="org.springframework.ws.soap.axiom.AxiomSoapMessageFactory"> 

     <property name="payloadCaching" value="true"/> 

    </bean> 


    <bean id="abstractClient" abstract="true"> 
     <constructor-arg ref="messageFactory"/> 
    </bean> 

    <bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/> 

<bean id="myWebServiceClient" parent="abstractClient" class="class.path.MyWsClient"> 

     <property name="defaultUri" value="http://your.webservice.url"/> 

     <property name="marshaller" ref="marshaller"/> 

     <property name="unmarshaller" ref="marshaller"/> 

    </bean> 

最後來看看WS-客戶端的Java類

public class MyWsClient extends WebServiceGatewaySupport { 
//if you need some Dao, Services, just @Autowired here. 

    public MyWsClient(WebServiceMessageFactory messageFactory) { 
     super(messageFactory); 
    } 

    // here is the operation defined in your wsdl 
    public Object someOperation(Object parameter){ 

     //instantiate the xmlbeans generated class, infact, the instance would be the document (marshaled) you are gonna send to the WS 

     SomePojo requestDoc = SomePojo.Factory.newInstance(); // the factory and other methods are prepared by xmlbeans 
     ResponsePojo responseDoc = (ResponsePojo)getWebServiceTemplate().marshalSendAndReceive(requestDoc); // here invoking the WS 


//then you can get the returned object from the responseDoc. 

    } 

}

我希望示例代碼是有幫助的。