在当今的软件开发领域,使用Spring框架来调用Web服务(尤其是SOAP服务)是一种非常常见的做法。WSDL(Web Services Description Language)是描述SOAP服务的标准语言。本文将详细介绍如何使用Spring框架轻松上手调用WSDL服务。
准备工作
在开始之前,请确保以下准备工作已经完成:
- Java开发环境:安装Java Development Kit(JDK)并配置环境变量。
- IDE:选择并安装一个IDE,如IntelliJ IDEA或Eclipse。
- Spring框架:下载并配置Spring框架的依赖库。
- WSDL文件:获取目标服务的WSDL文件。
步骤一:创建Spring项目
- 在IDE中创建一个新的Spring Boot项目。
- 添加以下依赖到
pom.xml文件中:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
</dependencies>
步骤二:配置WSDL客户端
- 在Spring Boot项目的
src/main/java目录下创建一个新的类,例如WsClientConfig.java。 - 在该类中,配置WSDL客户端:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.core.SoapFaultResolver;
@Configuration
public class WsClientConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("com.example.wsdemo");
return marshaller;
}
@Bean
public SoapFaultResolver soapFaultResolver() {
return new SoapFaultResolver() {
@Override
public boolean isFault(SoapFault fault) {
return true;
}
};
}
@Bean
public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
WebServiceTemplate template = new WebServiceTemplate();
template.setDefaultUri("http://example.com/wsdl");
template.setMarshaller(marshaller);
template.setUnmarshaller(marshaller);
return template;
}
}
- 在
WsClientConfig类中,marshaller方法用于配置JAXB(Java Architecture for XML Binding)的映射器,soapFaultResolver方法用于处理SOAP错误,webServiceTemplate方法用于创建一个用于调用WSDL服务的模板。
步骤三:调用WSDL服务
- 在Spring Boot项目的
src/main/java目录下创建一个新的类,例如WsClient.java。 - 在该类中,调用WSDL服务:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.springframework.ws.soap.client.core.SoapMessageCallback;
import org.springframework.ws.soap.client.core.SoapResponseCallback;
public class WsClient {
@Autowired
private WebServiceTemplate webServiceTemplate;
public void callService(String request) {
SoapMessageCallback requestCallback = message -> {
message.getSOAPBody().addDocument(new org.w3c.dom.DocumentImpl());
message.getSOAPBody().addMessageElement("http://example.com", "request", "ns1");
};
SoapResponseCallback responseCallback = response -> {
System.out.println("Response: " + response.getSOAPBody().getTextContent());
};
webServiceTemplate.sendSourceAndReceiveResponse("http://example.com/wsdl", requestCallback, responseCallback, new SoapActionCallback("http://example.com/action"));
}
}
- 在
WsClient类中,callService方法用于调用WSDL服务。它接受一个请求字符串,使用SoapMessageCallback设置请求消息,使用SoapResponseCallback处理响应消息,并使用SoapActionCallback设置SOAP操作。
步骤四:运行程序
- 在IDE中运行Spring Boot项目。
- 在项目的根目录下创建一个
src/main/resources目录,并在该目录下创建一个application.properties文件。 - 在
application.properties文件中添加以下配置:
spring.ws.server.endpoint-uri=http://localhost:8080/ws
- 在项目的根目录下创建一个
src/main/resources/META-INF目录,并在该目录下创建一个wsdl目录。 - 将WSDL文件复制到
wsdl目录下。
总结
通过以上步骤,您已经成功地使用Spring框架调用WSDL服务。希望本文能帮助您轻松上手,并在实际项目中应用所学知识。
