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

 

posted @ 2020-11-07 20:26  一沙世界  阅读(3419)  评论(0编辑  收藏  举报