"Go" solves the problem of the Ajax return JSON garbled in spring MVC

Source: Internet
Author: User

Reprinted from: http://my.oschina.net/u/140421/blog/176625

Reprinted from: http://my.oschina.net/candon/blog/149073?fromerr=I9VBOy62

First reprint:

In the use of SPRING-MVC MVC is not only enjoy the convenience it brings, but also headaches some of its problems, such as the classic Chinese garbled problem. Now is the data exchange format using JSON as the client and the server is very popular, but in Springmvc sometimes because of our various negligence to cause the page Ajax request to the corresponding data, if with Chinese is often "????". "It's annoying," he says. After the search on the internet there are many solutions, the scheme all say Chinese garbled problem involved in the Springmvc Stringhttpmessageconverter class, the answer is to write a class to inherit it, and then use a bunch of dazzling configuration to go in the Beans.xml configuration , and must also be configured before the <mvc:annotation-driven/> tag, otherwise ....

I also appeared JSON Chinese garbled problem, found a lot of schemes after exultation, one after another to test. The result was disappointing: no one can solve my problem! This is not strange, and so cumbersome process to be daunting, and to read the source code is covered by what, all say the reason lies in:

......    public class Stringhttpmessageconverter extends abstracthttpmessageconverter<string> {public            static final Charset default_charset = Charset.forname ("iso-8859-1");    ......    

I think this is really the main reason, then a lot of code and configuration, I look dazzled. I was wondering, is there an easy way? Let me find out. 2 Easy ways to do this:

1. Get response in action, write the response data by him.
        Response.setheader ("Cache-control", "No-cache");           Response.setcontenttype ("Text/json;charset=utf-8");          Response.setcharacterencoding ("UTF-8");          PrintWriter out = Response.getwriter ();          Out.write (result);          
2. Write in @requestmaping of Aciton method that needs to return JSON
@ResponseBody      @RequestMapping (value= "/list", produces = "text/html;charset=utf-8") public      String Getbooklist (httpservletresponse response) {String result = Bookservice.booklistfromjson (); return result;      }  

  

A second reprint:

Project with SPRINGMVC return JSON format, Chinese garbled, looked at Springmvc source found stringhttpmessageconverter This class default code for Iso-8859-1 (tragedy, Springmvc such a big thing how not utf-8, do not understand)

Here is the workaround,

SPRINGMVC configuration file:

<bean class= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property n Ame= "Messageconverters" > <list> <bean class = "Org.springframework.http.conver ter.                        Stringhttpmessageconverter "> <property name =" Supportedmediatypes "> <list> <value>text/html;charset=UTF-8</value> < /list> </property> </bean> </list> &L t;/property> </bean> <bean class= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappin Ghandleradapter "> <property name=" messageconverters "> <list> <bean class= "Org.springframework.http.converter.StringHttpMessageConverter" > <property name= "Supported       MediaTypes ">                  <list> <value>text/html; charset=utf-8</value> </list> </property> &L t;/bean> </list> </property> </bean> <bean class= "ORG.SPRINGFRAMEWORK.W              Eb.servlet.mvc.method.annotation.RequestMappingHandlerMapping "> <property name=" Interceptors ">   <list> </list> </property> </bean>

  

Use this configuration to delete Springmvc this <mvc:annotation-driven/>, do not use the SPRINGMVC default initialization configuration

"Go" solves the problem of the Ajax return JSON garbled in spring MVC

Related Article

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.