WebClient – 添加 defaultHeaders

huangapple go评论90阅读模式
英文:

WebClient - adding defaultHeaders

问题

this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(httpHeaders -> {
            HttpHeaders headers = createHeaders(token); // Replace 'token' with your actual token value
            httpHeaders.addAll(headers);
        }) // Consumer<HttpHeaders>
        .build();

请注意,您需要将上述代码中的token替换为您实际的令牌值。这段代码将使用createHeaders()方法创建的HttpHeaders对象添加到defaultHeaders中,以将多个标头添加到WebClient中。

英文:

I'm trying to put multiple headers into defaultHeaders(), But I don't have idea how to create Consumer object from return of createHeaders() method

this.someWebClient = WebClient.builder()
		.baseUrl(someConfiguration.getApiUrl())
		.clientConnector(buildTimeoutConnector())
		.defaultHeaders(????) // Consumer&lt;HttpHeaders&gt;
		.build();

I can build my header in that way:

private HttpHeaders createHeaders(String token) {
	HttpHeaders headers = new HttpHeaders();
	headers.add(HttpHeaders.ACCEPT, V1_PUBLIC);
	headers.add(HttpHeaders.HOST, &quot;abc&quot;);
	headers.add(HttpHeaders.AUTHORIZATION, &quot;Bearer &quot; + token);
	return headers;
}

but how to wrap it into Consumer?

答案1

得分: 15

清楚地说,方法文档指出它需要一个某种类型的Consumer。因此您可以创建一个实现了Consumer接口的匿名类,或者像这样使用lambda表达式

  1. 使用匿名内部类:
this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(new Consumer<HttpHeaders>() {
          @Override
          public void accept(HttpHeaders httpHeaders) {
            httpHeaders.addAll(createHeaders(token));
          }
        })
        .build();
  1. 使用lambda:
this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(httpHeaders -> {
          httpHeaders.addAll(createHeaders(token));
        })
        .build();
英文:

Clearly the method doc says that it needs a Consumer of some Type. So you can create an anonymous class implementing the Consumer interface or use lambda expression like this:

  1. Using anonymous inner class:
this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(new Consumer&lt;HttpHeaders&gt;() {
          @Override
          public void accept(HttpHeaders httpHeaders) {
            httpHeaders.addAll(createHeaders(token));
          }
        })
        .build();
  1. Using lambda:
this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(httpHeaders -&gt; {
          httpHeaders.addAll(createHeaders(token));
        })
        .build();

答案2

得分: 2

private void addDefaultHeaders(final HttpHeaders headers) {
    headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
    headers.add(HttpHeaders.ACCEPT, "application/json");
}

然后

this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(this::addDefaultHeaders)
        .build();
英文:
private void addDefaultHeaders(final HttpHeaders headers) {
    headers.add(HttpHeaders.CONTENT_TYPE, &quot;application/json&quot;);
    headers.add(HttpHeaders.ACCEPT, &quot;application/json&quot;);
  }

and then

this.someWebClient = WebClient.builder()
        .baseUrl(someConfiguration.getApiUrl())
        .clientConnector(buildTimeoutConnector())
        .defaultHeaders(this::addDefaultHeaders)
        .build();

答案3

得分: 1

使用以下代码:

    @Bean
    public WebClient webClientConfiguration() {
        return WebClient
                .builder()
                .clientConnector(new ReactorClientHttpConnector(HttpClient.create()
                        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
                        .responseTimeout(Duration.ofMillis(responseTimeout))
                ))
                .baseUrl(this.msConfigProperties.getApiUrl())
                .defaultHeaders(httpHeaders())
                .build();
    }

然后是消费者函数:

    private Consumer<HttpHeaders> httpHeaders(){
        return headers -> {
            headers.set(HEADER_CONTENT_TYPE, String.valueOf(MediaType.APPLICATION_JSON));
            headers.setBasicAuth(this.msConfigProperties.getUserName(),this.msConfigProperties.getPassword());
            headers.set(HEADER_ACCEPT_ENCODING, String.valueOf(MediaType.APPLICATION_JSON));
        };
    }
英文:

Use this.

    @Bean
    public WebClient webClientConfiguration() {
        return WebClient
                .builder()
                .clientConnector(new ReactorClientHttpConnector(HttpClient.create()
                        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
                        .responseTimeout(Duration.ofMillis(responseTimeout))
                ))
                .baseUrl(this.msConfigProperties.getApiUrl())
                .defaultHeaders(httpHeaders())
                .build();
    }

Then Consumer function

    private Consumer&lt;HttpHeaders&gt; httpHeaders(){
        return headers -&gt; {
            headers.set(HEADER_CONTENT_TYPE, String.valueOf(MediaType.APPLICATION_JSON));
            headers.setBasicAuth(this.msConfigProperties.getUserName(),this.msConfigProperties.getPassword());
            headers.set(HEADER_ACCEPT_ENCODING, String.valueOf(MediaType.APPLICATION_JSON));
        };
    }

huangapple
  • 本文由 发表于 2020年7月28日 22:16:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63136226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定