其他分享
首页 > 其他分享> > Springboot.WebClient添加多个请求头

Springboot.WebClient添加多个请求头

作者:互联网

headersMap 是 Map<String,String>的入参

...
Consumer<HttpHeaders> headersConsumer = httpHeaders -> {
            for (Map.Entry<String, String> entry : headersMap.entrySet()) {
                httpHeaders.add(entry.getKey(), entry.getValue());
            }
        };
        Mono<T> mono = CloudWebClient.getInstance().get(builder.getUrl()).post()
                .headers(headersConsumer)
                .syncBody(builder.getRequestBody())
                .retrieve()
                .bodyToMono(typeReference)
                .timeout(Duration.of(5, ChronoUnit.SECONDS))
                .doOnSuccess(response -> consumerResponse.accept(JSON.toJSONString(response)))
                .doOnError(exception -> consumerResponse.accept(JSON.toJSONString(StringUtils.substring(exception.getMessage(), 0, 500))));
        return mono.block();

 

标签:headersConsumer,Springboot,Map,accept,添加,consumerResponse,entry,httpHeaders,WebC
来源: https://www.cnblogs.com/yoyotl/p/13942226.html