记录下RestTemplate请求接口返回中文乱码---尝试4种方案终于解决

记录下RestTemplate请求接口返回中文乱码---尝试4中方案终于解决

package com.test;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.njia.constant.ConnectTimeoutConstant;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication(scanBasePackages = {"com.test"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public RestTemplate restTemplate(){
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(ConnectTimeoutConstant.connectTimeout);
        httpRequestFactory.setConnectTimeout(ConnectTimeoutConstant.connectTimeout);
        httpRequestFactory.setReadTimeout(ConnectTimeoutConstant.connectTimeout);
        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        //第四种方案,成功{"name":"item_full_title","value":"2020夏季新款女款连衣裙修身V领连衣裙韩版chic中长裙子"}
        restTemplate.getMessageConverters().clear();
        restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());

//第一种尝试方案,失败{"name":"item_full_title","value":"2020\u590f\u5b63\u65b0\u6b3e"}
//           restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

//第二种尝试方案,失败{"name":"item_full_title","value":"2020\u590f\u5b63\u65b0\u6b3e"}
//        List> httpMessageConverters = restTemplate.getMessageConverters();
//        httpMessageConverters.stream().forEach(httpMessageConverter -> {
//            if(httpMessageConverter instanceof StringHttpMessageConverter){
//                StringHttpMessageConverter messageConverter = (StringHttpMessageConverter) httpMessageConverter;
//                messageConverter.setDefaultCharset(Charset.forName("UTF-8"));
//            }
//        });

//第三种尝试方案,失败{"name":"item_full_title","value":"2020\u590f\u5b63\u65b0\u6b3e"}
//        StringHttpMessageConverter t = new StringHttpMessageConverter();
//        //设置为false就可以修改header中的accept-charset属性
//        t.setWriteAcceptCharset(false);
//        t.setDefaultCharset(StandardCharsets.UTF_8);
//        restTemplate.getMessageConverters().add(0,t);

        return restTemplate;
    }
}

各种搜索网上资料各种尝试终于ok了.......

 String itemId =itemResultDO.getItemId();
        String platform=itemResultDO.getPlatform();
       
        url="http://*******?itemId="+itemId+"&Type=0&platform="+platform+"&page=1&limit=10";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Cookie", "experimentation_subject_id=25967cf; Hm_lvt_05134tgtrgthhgr75612c=1619073450,1619430893; session=eyJfcGVyb32YXNzdc3QiOiJbXSIsInVzZXJuYW1lIe2UifQ.YJzHWw.N_HWzr4334Ie0VAI0zJA4434tc");

        ResponseEntity response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                new HttpEntity(headers),
                String.class);
        System.out.println("返回信息为: "+response.getBody());

返回的数据:

[2021-05-13 16:23:24,750],[INFO],{},org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/],Initializing Spring DispatcherServlet 'dispatcherServlet'
[2021-05-13 16:23:24,751],[INFO],{},org.springframework.web.servlet.DispatcherServlet,Initializing Servlet 'dispatcherServlet'
[2021-05-13 16:23:24,761],[INFO],{},org.springframework.web.servlet.DispatcherServlet,Completed initialization in 10 ms
返回信息为: {"code":0,"count":7,"data":[{"name":"item_id","value":"6917920649675846853"},{"name":"item_full_title","value":"2020夏季新款女款连衣裙修身V领连衣裙韩版chic中长裙子"},{"name":"item_price","value":63900},{"name":"item_pic_url","value":"https://a.vpimg3.com/upload/merchandise/pdcvis/107361/2019/0628/120/994f3b2d-2b2c-4d15-9356-9a9d2fd466ff.jpg"},{"name":"item_discount_price","value":12900},{"name":"guide_desc","value":"唯品自营"},{"name":"commission_rate","value":800}],"msg":"success"}

 

你可能感兴趣的:(测试开发自动化测试,测试,java开发工具类,乱码,java)