Web 服务

Spring Boot 提供 Web 服务自动配置,因此您只需定义您的 Endpoints

可以使用 spring-boot-starter-webservices 模块轻松访问 Spring Web 服务功能

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

  • 属性

  • YAML

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

使用 WebServiceTemplate 调用 Web 服务

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

以下代码显示了一个典型的示例

  • Java

  • Kotlin

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 会使用类路径上可用的 HTTP 客户端库来检测合适的基于 HTTP 的 WebServiceMessageSender。您也可以自定义读取和连接超时,如下所示

  • Java

  • Kotlin

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()
	}

}