在我以前的項目
,我實現了一個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.
}
}
我希望示例代碼是有幫助的。
一個很好的樣本可以在http://stackoverflow.com/questions/18641928/consume-webservice-service-in-找到彈簧-WS-使用-WSDL – 2013-11-24 15:30:03