Sometimes we find that we receive Chinese and return is a. This is really a problem with the egg, spring in the parsing string converter default encoding is actually iso-8859-1
/*** Implementation of {@linkHttpmessageconverter} that can read and write strings. * * <p>by default, this converter supports all media types ({@code& #42;& #47;& #42;}), * and writes with a {@codeContent-type} of {@codeText/plain}. This can is overridden * by setting the {@link#setSupportedMediaTypes Supportedmediatypes} property. * * @authorArjen Poutsma *@since3.0*/ Public classStringhttpmessageconverterextendsAbstracthttpmessageconverter<string> { Public Static FinalCharset default_charset = Charset.forname ("iso-8859-1");
Now that the problem has been found, it must be a way to change, different versions of spring as if the method is not the same, many of the online said is spring3.*, and now Spring4 early came out
Change the way you can refer to
Http://stackoverflow.com/questions/3616359/who-sets-response-content-type-in-spring-mvc-responsebody
Http://www.cnblogs.com/chenying99/archive/2012/04/17/2453017.html
I now use the Spring4.2.5, the above said several methods have tried, finally found only these two can
Method one, using (produces = "application/json; Charset=utf-8 "):
@RequestMapping (value= "/getusersbypage", produces = "application/json; Charset=utf-8 ")// @RequestMapping ("/getusersbypage ") @ResponseBody public String Getusersbypage ( String page,string rows,string text,httpservletrequest request,httpservletresponse response) {
Method Two, add in Spring-mvc.xml: (recommend this)
<!--set the encoding for message conversion to UTF-8 prevent Controller from returning garbled characters in Chinese - <Beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> < Propertyname= "Messageconverters"> <List> <Beanclass= "Org.springframework.http.converter.StringHttpMessageConverter"> < Propertyname= "Supportedmediatypes"> <List> <value>Text/html;charset=utf-8</value> </List> </ Property> </Bean> </List> </ Property> </Bean>
Both of these methods are verified without problems.
Springmvc using @responsebody to return JSON Chinese garbled