RestTemplate发起http请求中文乱码问题解决方案

Posted zjting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RestTemplate发起http请求中文乱码问题解决方案相关的知识,希望对你有一定的参考价值。

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        
        String jsonStr = JSONObject.toJSONString(params);
        
        HttpEntity formEntity = new HttpEntity( jsonStr ,headers);
        String result = restTemplate.getForObject(url, String.class);

闲话不多说,直接看代码

以上是关于RestTemplate发起http请求中文乱码问题解决方案的主要内容,如果未能解决你的问题,请参考以下文章