对于最新的稳定版本,请使用 Spring Boot 3.4.0spring-doc.cn

Web 服务

Spring Boot 提供 Web 服务自动配置,因此您所要做的就是定义@Endpoint bean。spring-doc.cn

Spring Web Services 功能可以通过该模块轻松访问。spring-boot-starter-webservicesspring-doc.cn

SimpleWsdl11DefinitionSimpleXsdSchema bean 可以分别为您的 WSDL 和 XSD 自动创建。 为此,请配置其位置,如以下示例所示:spring-doc.cn

spring.webservices.wsdl-locations=classpath:/wsdl
spring:
  webservices:
    wsdl-locations: "classpath:/wsdl"

使用 WebServiceTemplate 调用 Web 服务

如果需要从应用程序调用远程 Web 服务,可以使用 WebServiceTemplate 类。 由于WebServiceTemplate实例通常需要在使用之前进行自定义,因此 Spring Boot 不提供任何单个自动配置的WebServiceTemplate Bean。 但是,它确实会自动配置WebServiceTemplateBuilder,该构建器可用于在需要时创建WebServiceTemplate实例。spring-doc.cn

以下代码显示了一个典型的示例:spring-doc.cn

import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.core.SoapActionCallback;

@Service
public class MyService {

	private final WebServiceTemplate webServiceTemplate;

	public MyService(WebServiceTemplateBuilder webServiceTemplateBuilder) {
		this.webServiceTemplate = webServiceTemplateBuilder.build();
	}

	public SomeResponse someWsCall(SomeRequest detailsReq) {
		return (SomeResponse) this.webServiceTemplate.marshalSendAndReceive(detailsReq,
				new SoapActionCallback("https://ws.example.com/action"));
	}

}
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder
import org.springframework.stereotype.Service
import org.springframework.ws.client.core.WebServiceTemplate
import org.springframework.ws.soap.client.core.SoapActionCallback

@Service
class MyService(webServiceTemplateBuilder: WebServiceTemplateBuilder) {

	private val webServiceTemplate: WebServiceTemplate

	init {
		webServiceTemplate = webServiceTemplateBuilder.build()
	}

	fun someWsCall(detailsReq: SomeRequest?): SomeResponse {
		return webServiceTemplate.marshalSendAndReceive(
			detailsReq,
			SoapActionCallback("https://ws.example.com/action")
		) as SomeResponse
	}

}

默认情况下,WebServiceTemplateBuilder使用 Classpath 上可用的 HTTP 客户端库来检测合适的基于 HTTP 的WebServiceMessageSender。 您还可以自定义读取和连接超时,如下所示:spring-doc.cn

import java.time.Duration;

import org.springframework.boot.webservices.client.HttpWebServiceMessageSenderBuilder;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.transport.WebServiceMessageSender;

@Configuration(proxyBeanMethods = false)
public class MyWebServiceTemplateConfiguration {

	@Bean
	public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) {
		WebServiceMessageSender sender = new HttpWebServiceMessageSenderBuilder()
				.setConnectTimeout(Duration.ofSeconds(5))
				.setReadTimeout(Duration.ofSeconds(2))
				.build();
		return builder.messageSenders(sender).build();
	}

}
import org.springframework.boot.webservices.client.HttpWebServiceMessageSenderBuilder
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.ws.client.core.WebServiceTemplate
import java.time.Duration

@Configuration(proxyBeanMethods = false)
class MyWebServiceTemplateConfiguration {

	@Bean
	fun webServiceTemplate(builder: WebServiceTemplateBuilder): WebServiceTemplate {
		val sender = HttpWebServiceMessageSenderBuilder()
			.setConnectTimeout(Duration.ofSeconds(5))
			.setReadTimeout(Duration.ofSeconds(2))
			.build()
		return builder.messageSenders(sender).build()
	}

}