毋以其所不能疑,毋以其所能骄人。——佚名

代码如下,可以通过ExchangeStrategies指定序列化反序列化方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import com.namaste.config.JacksonObjectMapper;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {

@Bean
public WebClient.Builder webClientBuilder(ExchangeStrategies exchangeStrategies) {
return WebClient.builder().exchangeStrategies(exchangeStrategies);
}

@Bean("loadBalancedWebClientBuilder")
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder(ExchangeStrategies exchangeStrategies) {
return WebClient.builder().exchangeStrategies(exchangeStrategies);
}

@Bean
public ExchangeStrategies exchangeStrategies(JacksonObjectMapper objectMapper) {
ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(configurer -> {
configurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(objectMapper));
configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(objectMapper));
})
.build();
return strategies;
}
}