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();
上善若水,水利万物而不争。