英文:
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<HttpHeaders>
.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, "abc");
headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
return headers;
}
but how to wrap it into Consumer?
答案1
得分: 15
清楚地说,方法文档指出它需要一个某种类型的Consumer
。因此您可以创建一个实现了Consumer
接口的匿名类,或者像这样使用lambda表达式:
- 使用匿名内部类:
this.someWebClient = WebClient.builder()
.baseUrl(someConfiguration.getApiUrl())
.clientConnector(buildTimeoutConnector())
.defaultHeaders(new Consumer<HttpHeaders>() {
@Override
public void accept(HttpHeaders httpHeaders) {
httpHeaders.addAll(createHeaders(token));
}
})
.build();
- 使用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:
- Using anonymous inner class:
this.someWebClient = WebClient.builder()
.baseUrl(someConfiguration.getApiUrl())
.clientConnector(buildTimeoutConnector())
.defaultHeaders(new Consumer<HttpHeaders>() {
@Override
public void accept(HttpHeaders httpHeaders) {
httpHeaders.addAll(createHeaders(token));
}
})
.build();
- Using lambda:
this.someWebClient = WebClient.builder()
.baseUrl(someConfiguration.getApiUrl())
.clientConnector(buildTimeoutConnector())
.defaultHeaders(httpHeaders -> {
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, "application/json");
headers.add(HttpHeaders.ACCEPT, "application/json");
}
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<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));
};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论