在我之前的项目中,我使用 Spring 2.5.6、maven2、xmlbeans 实现了一个 Webservice 客户端。
- xmlbeans 负责 un/marshal
- maven2 用于项目管理/建筑等。
我在这里粘贴了一些代码,希望它们对您有所帮助。
xmlbeans maven 插件配置:(在 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>
因此,从上面的配置文件中,您需要将架构文件(无论是独立的还是在您的 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-client 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.
}
}
我希望示例代码对您有所帮助。