使用 wsdl 在 SPRING-WS 中使用 webservice 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18641928/
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
Consume webservice service in SPRING-WS using wsdl
提问by pandeis
I have WSDL with me .eg: /sample/hello?wsdl . I want to invoke the service the webservice by configuring in Spring-ws. I passed this wsdl as parameter to tags in springconfig.xml. Can anyone please tell me how to consume this webservice in Spring-ws.
我有 WSDL 。例如: /sample/hello?wsdl 。我想通过在 Spring-ws 中配置来调用 webservice 服务。我将此 wsdl 作为参数传递给 springconfig.xml 中的标签。谁能告诉我如何在 Spring-ws 中使用这个 web 服务。
回答by Taoufik Mohdit
1. Set up project dependencies
1.设置项目依赖
add the following dependencies to the pom file:
将以下依赖项添加到 pom 文件中:
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
</dependency>
2. Set up web service application context
2.设置web服务应用上下文
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.yourcomany.model" />
</bean>
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory" />
<property name="marshaller" ref="marshaller"></property>
<property name="unmarshaller" ref="marshaller"></property>
<property name="messageSender">
<bean
class="org.springframework.ws.transport.http.HttpComponentsMessageSender" />
</property>
<property name="defaultUri"
value="http://<hostname>:<portnumber>/sample/hello" />
</bean>
</beans>
3. Set up model classes which would map to your SOAP request/response objects
3. 设置将映射到您的 SOAP 请求/响应对象的模型类
For example, if your SOAP request XML looked like
例如,如果您的 SOAP 请求 XML 看起来像
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xxx="http://yourcomapny.com">
<soapenv:Header/>
<soapenv:Body>
<xxx:InputParameters>
<xxx:paramONE>1</xxx:paramONE>
</xxx:InputParameters>
</soapenv:Body>
</soapenv:Envelope>
and your SOAP response XML looked like:
并且您的 SOAP 响应 XML 如下所示:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header>
...
</env:Header>
<env:Body>
<xxx:OutputParameters xmlns:xxx="http://yourcompany.com">
<xxx:paramONE>0</xxx:paramONE>
</xxx:OutputParameters>
</env:Body>
</env:Envelope>
the corresponding classes (under the package you specified in the marshaller bean: com.yourcompany.model) would be respectively:
相应的类(在您在 marshaller bean 中指定的包下:com.yourcompany.model)将分别为:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "paramONE" })
@XmlRootElement(name = "InputParameters", namespace = "http://yourcompany.com")
public class InputParameters {
@XmlElement(required = true, namespace = "http://yourcompany.com")
private String paramONE;
public String getParamONE() {
return paramONE;
}
public void setParamONE(String paramONE) {
this.paramONE = paramONE;
}
}
and
和
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "paramONE" })
@XmlRootElement(name = "OutputParameters", namespace = "http://yourcompany.com")
public class OutputParameters {
@XmlElement(required = true, namespace = "http://yourcompany.com")
private BigDecimal paramONE;
public BigDecimal getParamONE() {
return this.paramONE;
}
public void setParamONE(BigDecimal paramONE) {
this.paramONE= paramONE;
}
}
4. Add an Object Factory (under package com.yourcompany.model) to create request/response objects
4. 添加一个对象工厂(在包 com.yourcompany.model 下)来创建请求/响应对象
@XmlRegistry
public class ObjectFactory {
public ObjectFactory() {
}
public InputParameters createYourRequest() {
return new InputParameters();
}
public OutputParameters createYourResponse() {
return new OutputParameters();
}
}
5. Create a client to consume the service
5. 创建一个客户端来消费服务
Interface:
界面:
public interface YourService {
BigDecimal getValue(String paramOne);
}
Implementation
执行
@Component("yourServiceClient")
public class YourServiceClient implements YourService {
private static final ObjectFactory WS_CLIENT_FACTORY = new ObjectFactory();
private WebServiceTemplate webServiceTemplate;
@Autowired
public YourServiceClient(WebServiceTemplate webServiceTemplate) {
this.webServiceTemplate = webServiceTemplate;
}
@Override
public BigDecimal getValue(String paramOne) {
InputParameters request = WS_CLIENT_FACTORY
.createYourRequest();
request.setParamONE(paramOne);
OutputParameters response = (OutputParameters) webServiceTemplate
.marshalSendAndReceive(request);
return response.getParamONE();
}
}
回答by Alireza Fattahi
@Taoufik Mohdit answer is complete!!
@Taoufik Mohdit 回答完毕!!
To build the input and output objects you can use Webservice-Client: Common approach with Spring WS, JAXB and just one WSDL file?to some how build these objects automatically
要构建输入和输出对象,您可以使用Webservice-Client:Spring WS、JAXB 和仅一个 WSDL 文件的通用方法?一些如何自动构建这些对象
回答by CodeNotFound
Given that this question is still active I thought I would post an update that reflects a number of changes that the recent version of the Spring Web Services framework and Spring in general introduce:
鉴于此问题仍然有效,我想我会发布一个更新,以反映 Spring Web 服务框架和 Spring 的最新版本一般引入的许多更改:
- The introduction of Spring Boot allows to leverage 'starter' POMs to simplify your Maven configuration. There is a specific
spring-boot-starter-web-servicesstarter for Spring-WS - Instead of specifying Spring configuration files using XML, Spring JavaConfigwas introduced which provides a type-safe, pure-Java option for configuring Spring.
- Generation of request/response objects based on a given WSDL file can be automated using Maven plugins. The plugin used by the Spring-WS examples is the
maven-jaxb2-plugin.
- Spring Boot 的引入允许利用“入门”POM 来简化您的 Maven 配置。
spring-boot-starter-web-servicesSpring-WS有一个特定的启动器 - 引入了Spring JavaConfig,而不是使用 XML 指定 Spring 配置文件,它提供了一个类型安全的纯 Java 选项来配置 Spring。
- 可以使用 Maven 插件自动生成基于给定 WSDL 文件的请求/响应对象。Spring-WS 示例使用的插件是
maven-jaxb2-plugin.
The WebServiceTemplateis still the core class for client-side Web service access. For more information you can check this detailed example on how to consume a web service using Spring-WS starting from a WSDL filethat I wrote.
的WebServiceTemplate仍然是客户端访问Web服务的核心类。有关更多信息,您可以查看有关如何使用 Spring-WS 从我编写的 WSDL 文件开始使用 Web 服务的详细示例。

