2010-03-06 25 views
6

tôi cần phải nhảy vào Dự án Dịch vụ Spring Web, trong đó tôi yêu cầu để thực hiện khách hàng các dịch vụ Web Spring Chỉ ..Web Service Xuân Khách hàng Hướng dẫn hoặc Ví dụ Required

Vì vậy, tôi đã trải qua với Spring's Client Reference Document.

Vì vậy, tôi có ý tưởng về các lớp bắt buộc để triển khai Ứng dụng khách. Tuy nhiên, vấn đề của tôi giống như tôi đã thực hiện một số googling, nhưng đã không nhận được bất kỳ ví dụ thích hợp của cả khách hàng và máy chủ từ đó tôi có thể thực hiện một mẫu cho khách hàng của tôi.

Vì vậy, nếu có ai cho tôi một số liên kết hoặc hướng dẫn cho ví dụ thích hợp từ đó tôi có thể tìm hiểu việc thực hiện phía khách hàng của tôi sẽ được đánh giá cao.

Cảm ơn bạn trước ...

+0

Một mẫu tốt có thể được tìm thấy tại http://stackoverflow.com/questions/18641928/consume-webservice-service-in- spring-ws-using-wsdl –

Trả lời

7

trong dự án trước đây của tôi, tôi đã triển khai ứng dụng khách Webservice với Spring 2.5.6, maven2, xmlbeans.

  • XMLBeans có trách nhiệm un/soái
  • maven2 là dành cho Quản lý đ.thoại dự án/xây dựng, vv

tôi dán một số mã ở đây và hy vọng họ là hữu ích.

XMLBeans maven Plugin conf: (trong 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> 

Vì vậy, từ conf ở trên, bạn cần phải đặt file schema (hoặc độc lập hoặc trong tập tin WSDL của bạn, bạn cần phải giải nén chúng và lưu dưới dạng tệp lược đồ.) dưới src/main/resources. khi bạn xây dựng dự án với maven, các pojos sẽ được tạo bởi xmlbeans. Mã nguồn được tạo sẽ dưới mục tiêu/nguồn được tạo/xmlbeans.

sau đó chúng tôi đến Spring conf. Tôi chỉ cần đặt bối cảnh có liên quan WS ở đây:

<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> 

cuối cùng, hãy nhìn ws-client java lớp

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. 

    } 

}

Tôi hy vọng các mã ví dụ rất hữu ích.

Các vấn đề liên quan