Java 需要 Spring Web Service 客户端教程或示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2391428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Web Service Client Tutorial or Example Required
提问by Nirmal
I need to jump into the Spring Web Service Project, in that I required to implement the Spring Web Service's Client Only..
我需要跳入 Spring Web 服务项目,因为我需要仅实现 Spring Web 服务的客户端。
So, I have already gone through with Spring's Client Reference Document.
所以,我已经完成了Spring 的 Client Reference Document。
So, I got the idea of required classes for the implementation of Client.
所以,我得到了实现 Client.js 所需的类的想法。
But my problem is like I have done some googling, but didn't get any proper example of both Client and Server from that I can implement one sample for my client.
但是我的问题就像我做了一些谷歌搜索,但没有得到任何正确的客户端和服务器示例,我可以为我的客户端实现一个示例。
So, if anybody gives me some link or tutorial for proper example from that I can learn my client side implementation would be greatly appreciated.
所以,如果有人给我一些链接或教程,我可以从中学习我的客户端实现,将不胜感激。
Thanks in advance...
提前致谢...
采纳答案by Kent
in my previous project, I implemented a Webservice client with Spring 2.5.6, maven2, xmlbeans.
在我之前的项目中,我用 Spring 2.5.6、maven2、xmlbeans 实现了一个 Webservice 客户端。
- xmlbeans is responsible for un/marshal
- maven2 is for project mgmt/building etc.
- xmlbeans 负责 un/marshal
- maven2 用于项目管理/建筑等。
I paste some codes here and hope they are helpful.
我在这里粘贴了一些代码,希望它们有帮助。
xmlbeans maven plugin conf: (in pom.xml)
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>
So from the above conf, you need to put the schema file (either standalone or in your WSDL file, you need to extract them and save as a schema file.) under src/main/resources. when you build the project with maven, the pojos are gonna be generated by xmlbeans. The generated sourcecodes will be under target/generated-sources/xmlbeans.
因此,从上面的 conf 中,您需要将架构文件(无论是独立的还是在您的 WSDL 文件中,您需要将它们提取并保存为架构文件。)在 src/main/resources 下。当您使用 maven 构建项目时,pojo 将由 xmlbeans 生成。生成的源代码将在 target/generated-sources/xmlbeans 下。
then we come to Spring conf. I just put the WS relevant context here:
然后我们来到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>
finally, take a look the ws-client java class
最后,看看 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.
}
}
}
I hope the example codes are helpful.
我希望示例代码有帮助。
回答by Shameer Kunjumohamed
Step by step tutorial on - Web Service Client with Spring-WS @ http://justcompiled.blogspot.com/2010/11/web-service-client-with-spring-ws.html
分步教程 - Web 服务客户端与 Spring-WS @ http://justcompiled.blogspot.com/2010/11/web-service-client-with-spring-ws.html