博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
请求SpringBoot 服务接口时的中文乱码问题
阅读量:4625 次
发布时间:2019-06-09

本文共 2237 字,大约阅读时间需要 7 分钟。

    此前部署的某SpringBoot的Restful风格服务接口,某个客户端进行请求时出现了中文乱码的问题。

    SpringBoot服务端代码类似如下:

 

    一开始采用了contentType="application/json;charset=utf-8"的解决方法:

//将xml转为json                JSONObject xmlToJson = XML.toJSONObject(readXmlContent.toString());                //设置缩进                String jsonString = xmlToJson.toString(4);                //输出格式化后的json                logger.info(jsonString);                        String contentType = "application/json;charset=UTF-8";                String result = HttpClientUtil.sendPostRequest(jsonString, pmUrl, contentType);

 其中HttpClientUtil.sendPostRequest的方法如下

/**     * 向目标url发送Post请求     *     * @param jsonString  请求体Json字符串     * @param targetUrl  目标URL     * @param contentType  contentType     * @return     */    public static String sendPostRequest(String jsonString, String targetUrl, String contentType) {        StringBuilder result = new StringBuilder();        try {            DefaultHttpClient httpClient = new DefaultHttpClient();            HttpPost postRequest = new HttpPost(targetUrl);            StringEntity input = new StringEntity(jsonString);            input.setContentType(contentType);            postRequest.setEntity(input);            HttpResponse response = httpClient.execute(postRequest);            if (response.getStatusLine().getStatusCode() != 200) {                result.append("HTTP Failed, error code : ").append(response.getStatusLine().getStatusCode());            }            BufferedReader br = new BufferedReader(                    new InputStreamReader((response.getEntity().getContent())));            String output;            logger.info("receive result from Server:{}  \n", targetUrl);            while ((output = br.readLine()) != null) {                result.append(output).append("\n");                logger.info(output);            }            httpClient.getConnectionManager().shutdown();        } catch (Exception e) {            logger.error(e.getMessage(), e);        }        return result.toString();    }}

    然后......继续中文乱码,被打脸了。左思右想总觉得哪里不对,代码逐行看过去,终于发现。。。

    没错,就是这个,将请求体封装成StringEntity的时候,要指定编码

    so,关键点根本不在ContentType上, 想当然的解决方案未必能解决实际问题。

    即使问题很小知识很基础,但是见小知大,仍然值得吸取教训。

    

 

转载于:https://www.cnblogs.com/zsfh/p/7689311.html

你可能感兴趣的文章
重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载...
查看>>
SQL中拆分字符串substr及统计字符出现频数replace用法实例讲解
查看>>
java实现MD5加密
查看>>
Xmind ZEN破解版来袭:如何去除水印
查看>>
iOS开发之蓝牙通信
查看>>
datepicker 时间戳转换问题
查看>>
程序硬件pcDuino裸板程序-led
查看>>
线程任务java并发包小结(二)
查看>>
编译代码在Android中使用JNI调用Opencv本地代码 配置方式 边缘检测 范例代码
查看>>
ALL about SYSDBA and SYSOPER Privileges in Oracle [ID 50507.1]
查看>>
JSON.parseObject笔记
查看>>
倒车入库太难了?掌握技巧,其实它也可以很简单
查看>>
怎么开手动档轿车?这么开让你再也不想开自动档
查看>>
FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和XmlWebApplicationContext简介
查看>>
java native方法及JNI实例
查看>>
9_2019.04.20随记
查看>>
29 最小的K个数
查看>>
10款免费且开源的项目管理工具
查看>>
thinkphp数据查询方法总结select ,find,getField,query
查看>>
Spring Boot控制上传文件大小
查看>>