SpringBoot3.x学习
0
SpringBoot3.x
支持了很多新的特性:native
/JDK17
/WebClient
/RestClient
/HTTP Interface
等等。
总结一些问题:
RestClient
乱码
final RestClient client = RestClient.builder().baseUrl("https://www.acgist.com")
.messageConverters((list) -> {
list.forEach(v -> {
if(v instanceof StringHttpMessageConverter x) {
x.setDefaultCharset(StandardCharsets.UTF_8);
}
});
})
.build();
WebClient
/RestClient
/RestTemplate
@Bean
@ConditionalOnMissingBean
RestClient.Builder restClientBuilder(RestClientBuilderConfigurer restClientBuilderConfigurer) {
final RestClient.Builder builder = RestClient.builder()
.baseUrl("https://www.acgist.com")
.requestFactory(ClientHttpRequestFactories.get(ClientHttpRequestFactorySettings.DEFAULTS));
return restClientBuilderConfigurer.configure(builder);
}
@Bean
public HttpClient httpClient(RestClient.Builder builder) {
final RestClient client = builder.build();
final RestClientAdapter adapter = RestClientAdapter.create(client);
final HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
return factory.createClient(HttpClient.class);
}
@Bean
public HttpClient httpClientWeb() {
final WebClient client = WebClient.builder().baseUrl("https://www.acgist.com").build();
final WebClientAdapter adapter = WebClientAdapter.create(client);
final HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
return factory.createClient(HttpClient.class);
}
@Bean
public HttpClient httpClientRest() {
final RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("https://www.acgist.com"));
restTemplate.getMessageConverters().forEach(v -> {
if(v instanceof StringHttpMessageConverter x) {
x.setDefaultCharset(StandardCharsets.UTF_8);
}
});
final RestTemplateAdapter adapter = RestTemplateAdapter.create(restTemplate);
final HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
return factory.createClient(HttpClient.class);
}