Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What should I do if SpringMvc returns @ ResponseBody for garbled Chinese characters?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

SpringMvc returns @ ResponseBody what to do when Chinese garbled occurs. I believe many inexperienced people are at a loss about this. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Use the @ ResponseBody of SpringMvc to return the specified data type as http body output. The content returned in the browser contains Chinese and garbled codes. The project code and tomcat code have been set to utf-8. The following returns a string of garbled codes.

Java code

@ RequestMapping ("user/get_comment_list.do")

Public @ ResponseBody String getUserCommentList (Integer user_id,Byte type) {

HashMap map = new HashMap ()

Map.put ("type", type)

Map.put ("user_id", user_id)

CommentActPojo actPojo = new CommentActPojo ()

List list = this.bo.getComList (map)

ActPojo.setComments (list)

/ / System.out.println ("data:" + JsonUtil.toJson (actPojo)); / / print data without Chinese garbled

Return JsonUtil.toJson (actPojo)

}

The version used by SpringMvc is 3.2.2. Later, some information was found on the Internet, and the returned data type and character encoding were forcibly specified in @ RequestMapping. The Chinese garbled was solved as follows:

Java code

@ RequestMapping (value= "user/get_comment_list.do", produces = "application/json; charset=utf-8")

Download the source code of the most mainstream Java background SSM framework springmvc spring mybatis project

The problem is that if there are many requests like this in the project, it will be cumbersome and tedious to configure produces for each request. After looking at the source code, it is found that the class org.springframework.http.converter.StringHttpMessageConverter is involved in spring's processing of ResponseBody, which sets defaultCharset to ISO-8859-1 in the default implementation. When the method of the @ RequestMapping tag is not configured with the produces attribute, the default encoding is automatically used; if the produces attribute is configured, the write method in AbstractHttpMessageConverter is not affected by supportedMediaTypes, and the header set by produce is assigned to contenttype. Modify the configuration of RequestMappingHandlerAdapter. The springMvc.xml is as follows:

Java code

Text/plain;charset=UTF-8

Text/html;charset=UTF-8

Applicaiton/javascript;charset=UTF-8

The above springMvc.xml file resets the encoding of StringHttpMessageConverter without having to set the produces property in each @ RequestMapping. If you return data of type Jackson, you can set it as follows:

Java code

Application/json; charset=UTF-8

Application/x-www-form-urlencoded; charset=UTF-8

After reading the above, have you mastered the method of Chinese garbled code when SpringMvc returns @ ResponseBody? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report