SpringBoot中,RestTemplate中文乱码解决方案
程序员文章站
2022-05-30 23:42:16
...
1、new一个RestTemplate:
SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(35000);
httpRequestFactory.setConnectTimeout(5000);
RestTemplate restTemplate = new RestTemplate(httpClientFactory);
2、重新设置restTemplate的messageConverters,否则用的默认的messageConverters。
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new ByteArrayHttpMessageConverter());
/** 解决乱码的converter */
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName
("UTF-8"));
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
RestTemplate restTemplate = new RestTemplate(httpClientFactory);
restTemplate.setMessageConverters(messageConverters);
上一篇: 查看数据表的基本结构
下一篇: C# DataTable 用法小结