我和我一起使用WSDL .eg:/ sample / hello?wsdl。我想通过在Spring-ws中配置来调用webservice服务。我将此wsdl作为参数传递给springconfig.xml中的标记。 谁能告诉我如何在Spring-ws中使用这个web服务。
答案 0 :(得分:48)
将以下依赖项添加到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>
<?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>
例如,如果您的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>
,您的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>
相应的类(在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;
}
}
和
@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;
}
}
@XmlRegistry
public class ObjectFactory {
public ObjectFactory() {
}
public InputParameters createYourRequest() {
return new InputParameters();
}
public OutputParameters createYourResponse() {
return new OutputParameters();
}
}
接口:
public interface YourService {
BigDecimal getValue(String paramOne);
}
实施
@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();
}
}
答案 1 :(得分:2)
@Taoufik Mohdit的答案已经完成!!
要构建输入和输出对象,您可以使用Webservice-Client: Common approach with Spring WS, JAXB and just one WSDL file?来了解如何自动构建这些对象
答案 2 :(得分:0)
鉴于此问题仍处于活动状态,我认为我会发布一个更新,该更新反映了最新版本的Spring Web Services框架和Spring一般介绍的一些更改:
filename: app.js
read: app.js
filename: main.js
read: main.js
filename: SharedPreferences.js
read: SharedPreferences.js
filename: KeyConstants.js
read: KeyConstants.js
success
启动器
spring-boot-starter-web-services
。 maven-jaxb2-plugin
仍然是客户端Web服务访问的核心类。有关详细信息,请查看我写的detailed example on how to consume a web service using Spring-WS starting from a WSDL file。