Spring RestTemplate调用接口乱码解决

 

 resttemplate乱码

解决方案:

参考:Spring RestTemplate 调用天气预报接口乱码的解决

一:

        RestTemplate restTemplate = new RestTemplate(
new HttpComponentsClientHttpRequestFactory()); // 使用HttpClient,支持GZIP
restTemplate.getMessageConverters().set(1,
new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 支持中文编码

String body = restTemplate.getForObject("https://devapi.qweather.com/v7/weather/3d?location=101010100&key=7f39185f15344739b9304168b55ce56c", String.class);

 

二:

maven 依赖

<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>


Connection.Response execute = Jsoup.connect("https://devapi.qweather.com/v7/weather/3d?location=101010100&key=7f39185f15344739b9304168b55ce56c").ignoreContentType(true).execute();
JSONObject jsonObject = JSONObject.parseObject(execute.body());
System.out.println(jsonObject.getString("code"));
posted @ 2021-04-16 11:09  _Phoenix  阅读(268)  评论(0编辑  收藏  举报