Resolve SPRING-MVC @responseBody Comments return JSON garbled problem

Source: Internet
Author: User

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:

[Java]
    1. ......
    2. public class Stringhttpmessageconverter extends Abstracthttpmessageconverter<string> {
    3. public static final Charset Default_charset = Charset.forname ("iso-8859-1");
    4. ......


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.
    1. Response.setheader ("Cache-control", "No-cache");
    2. Response.setcontenttype ("Text/json;charset=utf-8");
    3. Response.setcharacterencoding ("UTF-8");
    4. PrintWriter out = Response.getwriter ();
    5. Out.write (result);


2. Write in @requestmaping of Aciton method that needs to return JSON
    1. @ResponseBody
    2. @RequestMapping (value= "/list", produces = "text/html;charset=utf-8")
    3. Public String getbooklist (httpservletresponse response) {string result = Bookservice.booklistfromjson (); return result;
    4. }



So incredibly also the perfect solution to my problem, but also do not have to cover the source of the method, but also save a lot of configuration, the exhilaration?
But the solution I'm looking at, I think the most detailed and best is the guy's article: Click the Open link
I did not solve my problem according to his solution, but it was instructive.

Resolve SPRING-MVC @responseBody Comments return JSON garbled problem

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.