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]
- ......
- 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;
- }
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