Spring boot Spring WebClient执行https调用
有人知道如何配置WebClient以创建HTTPS端点吗 我的配置如下所示:Spring boot Spring WebClient执行https调用,spring-boot,spring-webflux,spring-webclient,Spring Boot,Spring Webflux,Spring Webclient,有人知道如何配置WebClient以创建HTTPS端点吗 我的配置如下所示: @Bean @NonNull public WebClient webClient() throws SSLException { final SslContext context = SslContextBuilder.forClient() .trustManager(InsecureTrustManagerFactory.INSTANCE)
@Bean
@NonNull
public WebClient webClient() throws SSLException {
final SslContext context = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
final HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(context));
return WebClient
.builder()
.exchangeStrategies(ExchangeStrategies.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024))
.build())
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
}
下面是访问HTTPS端点的方法
@Nullable
public AccessToken getAccessToken() {
return webClient
.post()
.uri(uriBuilder -> uriBuilder.path(authUrl)
.queryParam("username", username)
.queryParam("password", password)
.queryParam("client_id", clientId)
.queryParam("client_secret", clientSecret)
.queryParam("grant_type", "password")
.build())
.header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.exchange()
.flatMap(response -> {
//Error handling
if (response.statusCode().isError()) {
logger.error("error occured while authentication: {}", response.statusCode());
return response.createException().flatMap(Mono::error);
}
return response.bodyToMono(AccessToken.class);
})
.subscribeOn(Schedulers.elastic())
.block();
}
这是我的回答,不幸的是,我不允许显示所有的细节,因为有安全的数据。
所以我检查了所有内容,比如URL、参数,看起来都很好。同样,如果对restTemaple执行相同的操作,它也会起作用
Caused by: java.net.UnknownHostException: https:
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:798)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ Request to POST https:/<here goes secured endpoint with query parameters>
原因:java.net.UnknownHostException:https:
位于java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:798)
抑制:reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
在以下站点发现错误:
|_检查站⇢ 请求发布https:/
也许可以help@AmirSchnell正如您可能看到的,这已经像url上的示例中那样进行了配置,但它没有帮助您确定url是正确的吗UnknownHostException
是由java无法从提供的主机名解析IP地址引起的。本质上DNS查找失败。问题是您无法共享错误的部分:主机名。这让我很难帮忙,也许我能help@AmirSchnell正如您可能看到的,这已经像url上的示例中那样进行了配置,但它没有帮助您确定url是正确的吗UnknownHostException
是由java无法从提供的主机名解析IP地址引起的。本质上DNS查找失败。问题是您无法共享错误的部分:主机名。这使帮助变得很困难。