在我之前的项目中,我用Spring 2.5.6,maven2,xmlbeans实现了一个Web服务客户端。
- xmlbeans负责un / marshal
- maven2用于项目mgmt / building等。
我在此处粘贴一些代码,希望对您有所帮助。
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.prehaus.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.prehaus.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" > <property name="payloadCaching" value="true"/> </bean> <bean id="abstractClient" abstract="true"> <constructor-arg ref="messageFactory"/> </bean> <bean id="marshaller" /> <bean id="myWebServiceClient" parent="abstractClient" > <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. }}
我希望示例代码对您有所帮助。



