SPRINGMVC return @responsebody Chinese garbled

Source: Internet
Author: User
Tags tojson


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 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>

  1. @RequestMapping ("User/get_comment_list.do")

  2. Public @ResponseBody String getusercommentlist (Integer user_id,byte type) {

  3. hashmap<string, object> map = new hashmap<string, object> ();

  4. Map.put ("type", type);

  5. Map.put ("user_id", user_id);

  6. Commentactpojo Actpojo = new Commentactpojo ();

  7. list<commentinfo> list = this. bo.getcomlist (map);

  8. Actpojo.setcomments (list);

  9. //system.out.println ("Data:" +jsonutil.tojson (Actpojo));//Print Data no Chinese garbled

  10. return Jsonutil.tojson (Actpojo);

  11. }



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 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>

    1. @RequestMapping (Value= "user/get_comment_list.do" , Produces =  " Application/json; charset=utf-8 ")   


Java code 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="Http://www.springframework.org/schema/beans"

  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http// Www.springframework.org/schema/context "

  4. xmlns:mvc="Http://www.springframework.org/schema/mvc"

  5. Xsi:schemalocation= "http://www.springframework.org/schema/beans

  6. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

  7. http://www.springframework.org/schema/context

  8. http://www.springframework.org/schema/context/spring-context-3.2.xsd

  9. http://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC

  10. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">

  11. <!--must be in <mvc:annotation-driven> before--

  12. <bean Class="Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter ">

  13. <property name="Messageconverters">

  14. <list>

  15. <bean Class="Org.springframework.http.converter.StringHttpMessageConverter">

  16. <property name="Supportedmediatypes">

  17. <list>

  18. <value>text/plain;charset=utf-8</value>

  19. <value>text/html;charset=utf-8</value>

  20. <value>applicaiton/javascript;charset=utf-8</value>

  21. </list>

  22. </property>

  23. </bean>

  24. </list>

  25. </property>

  26. </bean>

  27. <!--scan project Files--

  28. <context:component-scan base-package ="Com.tcl.club.core" />

  29. <context:component-scan base-package ="Com.cus.back" >

  30. <context:exclude-filter type="annotation" expression="Org.springframework.stereotype.Service" />

  31. </context:component-scan>

  32. <mvc:annotation-driven/>

  33. <bean id="Multipartresolver"

  34. class="Org.springframework.web.multipart.commons.CommonsMultipartResolver" />

  35. <!--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-

  36. <bean id="Viewresolver"

  37. class="Org.springframework.web.servlet.view.InternalResourceViewResolver">

  38. <property name="Viewclass"

  39. value="Org.springframework.web.servlet.view.JstlView" />

  40. <property name="prefix" value="/web-inf/view/" />

  41. <property name="suffix" value=". jsp" />

  42. </bean>

  43. </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 650) this.width=650; "class=" star "src=" Http://assen.iteye.com/images/icon_star.png "alt=" Favorite Code "style=" border : 0px; "/>

  1. <bean

  2. class="Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

  3. <property name="Supportedmediatypes">

  4. <list>

  5. <value>application/json; charset=utf-8</value>

  6. <value>application/x-www-form-urlencoded; charset=utf-8</value>

  7. </list>

  8. </property>

  9. </bean>




SPRINGMVC return @responsebody Chinese garbled

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.