zoukankan      html  css  js  c++  java
  • RestTemplate调用三方接口获取数据时出现乱码的常见解决办法

    查看响应数据的编码格式是否正确
    • 若不正确可在RestTemplate注入spring容器时进行修改,如下
    package com.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.client.RestTemplateBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.client.RestTemplate;
    import java.nio.charset.Charset;
    
    @Configuration
    public class RestTemplateConfig {
    
        @Autowired
        private RestTemplateBuilder builder;
        
        @Bean
        public RestTemplate restTemplate() {
            RestTemplate restTemplate = builder.build();
            restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(Charset.forName("ISO-8859-1")));
            return restTemplate;
        }
        
    }
    • 或者直接修改返回的数据编码格式
    dataStr=new String(dataStr.getBytes("UTF-8"),"ISO-8859-1");
    查看接口返回的内容编码是否为gzip

    在这里插入图片描述

    • 若为gzip则进行解码,如下
        /**
         *
         * @param url 第三方接口
         * @return
         */
        private Weather doGetData(String url) {
            //头部信息定义end
            try {
                ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
    
                if(response.getStatusCode() == HttpStatus.OK) {
                    //开始gzip解码
                    InputStream inWithCode = new ByteArrayInputStream(response.getBody().getBytes("ISO-8859-1"));
                    GZIPInputStream gunzip = new GZIPInputStream(inWithCode);
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] buffer = new byte[256];
                    int n;
                    while ((n = gunzip.read(buffer))>= 0) {
                        out.write(buffer, 0, n);
                    }
                    //gzip解码结束
                    String dataStr = out.toString();
                    //将数据封装到WeatherResponse对象中
                    WeatherResponse wr = new ObjectMapper().readValue(dataStr, WeatherResponse.class);
                    if(wr.getStatus().equals("1000")) {
                        //返回响应数据
                        return wr.getData();
                    }else {
                        throw new BusinessException("call weather api error");
                    }
                }else {
                    throw new BusinessException("call weather api error");
                }
            }catch(BusinessException e) {
                throw e;
            }catch(Exception e) {
                throw new BusinessException("call weather api error", e);
            }
        }
  • 相关阅读:
    新版淘淘商城_01_简介
    JavaMail之-通过邮件激活账号
    javaMail发送邮件
    JavaMail学习之一-邮件传输协议
    解决ios的safari不能自动播放audio问题(以及部分微信也不能自动播放)
    css3背景渐变色
    jq杂记
    各种“分享按钮“方法总结
    底部导航统一高度
    js 与或运算符 || && 妙用
  • 原文地址:https://www.cnblogs.com/mogong/p/12571344.html
Copyright © 2011-2022 走看看