Use SPRINGMVC @responsebody return the type of the specified data as the HTTP body outward output, the content returned in the browser has Chinese, will appear garbled, the project encoding, tomcat encoding, etc. have been set to Utf-8, the following is a string of Chinese garbled.
Java code
- @RequestMapping ("user/get_comment_list.do")
- public @ResponseBody String getusercommentlist (Integer user_id,byte type) {
- hashmap<string, object> map = new hashmap<string, object> ();
- Map.put ("type", type);
- Map.put ("user_id", user_id);
- Commentactpojo Actpojo = new Commentactpojo ();
- list<commentinfo> list = this.bo.getComList (map);
- Actpojo.setcomments (list);
- //system.out.println ("Data:" +jsonutil.tojson (Actpojo));//Print Data no Chinese garbled
- return Jsonutil.tojson (Actpojo);
- }
Springmvc use of the version is 3.2.2, later on the internet to find some information, in the @requestmapping mandatory to specify the type of data returned and character encoding, Chinese garbled solution, as follows:
Java code
- @RequestMapping (value="User/get_comment_list.do", produces = "Application/json; Charset=utf-8 ")
The most mainstream Java background SSM framework SPRINGMVC Spring MyBatis Project Source
The problem is, if there are many similar requests in the project, each request to configure produces, it will be cumbersome and cumbersome, look at the source code, It is found that the Org.springframework.http.converter.StringHttpMessageConverter class is involved in spring processing of responsebody, which in the default implementation sets Defaultcharset to iso- 8859-1. The default encoding is automatically used when the @requestmapping tagged method is not configured with the produces property, and if the produces property is configured, The Write method in Abstracthttpmessageconverter will not be affected by supportedmediatypes, and the header set with produce is assigned to ContentType. Change the configuration of Requestmappinghandleradapter, Springmvc.xml as follows:
Java code
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "http://www.springframework.org/ Schema/context "
- xmlns:mvc="Http://www.springframework.org/schema/mvc"
- Xsi:schemalocation= "http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
- <!--must be in <mvc:annotation-driven> before--
- <bean class="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
- <property name="Messageconverters" >
- <list>
- <bean class="Org.springframework.http.converter.StringHttpMessageConverter" >
- <property name="Supportedmediatypes" >
- <list>
- <value>text/plain;charset=utf-8</value>
- <value>text/html;charset=utf-8</value>
- <value>applicaiton/javascript;charset=utf-8</value>
- </list>
- </property>
- </bean>
- </list>
- </property>
- </bean>
- <!--scan project Files--
- <context:component-scan base-package="Com.tcl.club.core"/>
- <context:component-scan base-package="Com.cus.back" >
- <context:exclude-filter type="annotation" expression="Org.springframework.stereotype.Service"/>
- </context:component-scan>
- <mvc:annotation-driven/>
- <bean id="Multipartresolver"
- class="Org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
- <!--the resolution of the model view name by adding a prefix to the Model view name, which is automatically called after the address entered by the Requestmapping for view resolution-
- <bean id="Viewresolver"
- class="Org.springframework.web.servlet.view.InternalResourceViewResolver" >
- <property name="Viewclass"
- value="Org.springframework.web.servlet.view.JstlView"/>
- <property name="prefix" value="/web-inf/view/"/>
- <property name="suffix" value=". jsp"/>
- </bean>
- </beans>
The Springmvc.xml file above re-sets the encoding of the stringhttpmessageconverter without having to set the produces attribute in each @requestmapping. If you return the type of Jackson data, you can set it as follows:
Java code
- <bean
- class="Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >
- <property name="Supportedmediatypes" >
- <list>
- <value>application/json; charset=utf-8</value>
- <value>application/x-www-form-urlencoded; charset=utf-8</value>
- </list>
- </property>
- </bean>
SPRINGMVC return @responsebody Chinese garbled