2010-03-06 85 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. 

    } 

}

我希望示例代码是有帮助的。