Spring uses AnnotationMethodHandler Adapter's handleResponseBody method, AnnotationMethodHandler Adapter matches the value of "Accept" in request header with MediaType supported by message Converter, and then writes "Content-Type" in response with the first value of "Accept". Generally, requests are made through browsers.
The value of "Accept" in header is generated by the browser.
Someone tracked the implementation class of @ResponseBody and found that its default encoding was iso-8859-1, so it is obvious that the Chinese returned by ajax acceptance server must be scrambled.
The following two solutions are provided:
Method 1
When I came across this problem, I consulted the data and adopted a relatively simple method to solve this problem, that is, when the server returns to Chinese, it does not use this annotation, but directly uses the object of HttpServletResponse to complete the transmission. On the server side, it can set the coding type by response.setContentType("text/plain;charset=UTF-8"). In this way, there will be no Chinese scrambling.
The server-side core code is as follows:
-
@RequestMapping(value = "test", method = RequestMethod.POST)
-
public void test(HttpServletRequest request,
-
HttpServletResponse response) {
-
String result = null;
-
-
String userName = request.getParameter("userName");
-
-
result = "Hello!";
-
-
PrintWriter out = null;
-
response.setContentType("text/plain;charset=UTF-8");
-
try {
-
out = response.getWriter();
-
out.write(result.toString());
-
} catch (IOException e) {
-
e.printStackTrace();
-
} finally {
-
out.close();
-
}
-
}
When returning a value, it is set according to its own data type. Commonly used are:
response.setContentType("text/html; charset=utf-8"); html
response.setContentType("text/plain; charset=utf-8"); text
response.setContentType("application/json; charset=utf-8"); data
response.setContentType("application/xml; charset=utf-8"); xml
Method two
2014-07-11
Today I look up this problem again, and I have a better solution to it. spring The BeanPostProcessor interface is implemented and a new class is created in its own project, as follows:
-
package springmvc.extention;
-
-
import java.nio.charset.Charset;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import org.springframework.beans.BeansException;
-
import org.springframework.beans.factory.config.BeanPostProcessor;
-
import org.springframework.http.MediaType;
-
import org.springframework.http.converter.StringHttpMessageConverter;
-
-
-
-
-
public class UTF8StringBeanPostProcessor implements BeanPostProcessor {
-
@Override
-
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
-
if (bean instanceof StringHttpMessageConverter) {
-
MediaType mediaType = new MediaType("text", "plain", Charset.forName("UTF-8"));
-
List<MediaType> types = new ArrayList<MediaType>();
-
types.add(mediaType);
-
((StringHttpMessageConverter) bean).setSupportedMediaTypes(types);
-
}
-
return bean;
-
}
-
-
@Override
-
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
-
return bean;
-
}
-
}
Then you can register the bean in your Spring configuration file, try your own program, and find that the problem has been solved.
-
<! - Resolve Chinese scrambling using @ResponseBody. >
-
<bean class="springmvc.extention.UTF8StringBeanPostProcessor"></bean>
(Original address: http://blog.csdn .NET/zhshulin)