Web Services
Spring Boot 提供 Web Services 自动配置,因此你只需定义你的 @Endpoint
bean 即可。
通过 Spring Web Services 功能 可以轻松访问 spring-boot-starter-webservices
模块。
可以为你的 WSDL 和 XSD 自动创建 SimpleWsdl11Definition
和 SimpleXsdSchema
bean。为此,请配置它们的位置,如下例所示
-
属性
-
YAML
spring.webservices.wsdl-locations=classpath:/wsdl
spring:
webservices:
wsdl-locations: "classpath:/wsdl"
使用 WebServiceTemplate 调用 Web Services
如果你的应用程序需要调用远程 Web Services,你可以使用 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.http.client.ClientHttpRequestFactorySettings;
import org.springframework.boot.webservices.client.WebServiceMessageSenderFactory;
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;
@Configuration(proxyBeanMethods = false)
public class MyWebServiceTemplateConfiguration {
@Bean
public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) {
ClientHttpRequestFactorySettings settings = ClientHttpRequestFactorySettings.defaults()
.withConnectTimeout(Duration.ofSeconds(2))
.withReadTimeout(Duration.ofSeconds(2));
builder.httpMessageSenderFactory(WebServiceMessageSenderFactory.http(settings));
return builder.build();
}
}
import org.springframework.boot.http.client.ClientHttpRequestFactorySettings
import org.springframework.boot.webservices.client.WebServiceMessageSenderFactory
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 settings = ClientHttpRequestFactorySettings.defaults()
.withConnectTimeout(Duration.ofSeconds(2))
.withReadTimeout(Duration.ofSeconds(2))
builder.httpMessageSenderFactory(WebServiceMessageSenderFactory.http(settings))
return builder.build()
}
}
如果未应用特定的模板自定义代码,你还可以更改所使用的 全局 HTTP 客户端配置。 |