SSM Framework - Spring MVC uses @ResponseBody annotation to return value, Ajax obtains Chinese scrambling solution

Keywords: Spring xml Java encoding

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:

  1. @RequestMapping(value = "test", method = RequestMethod.POST)  
  2.     public void test(HttpServletRequest request,  
  3.             HttpServletResponse response) {  
  4.         String result = null;  
  5.         //Get the value from the client  
  6.         String userName = request.getParameter("userName");  
  7.         //Return a sentence to the client  
  8.         result = "Hello!";  
  9.   
  10.         PrintWriter out = null;  
  11.         response.setContentType("text/plain;charset=UTF-8");  
  12.         try {  
  13.             out = response.getWriter();  
  14.             out.write(result.toString());  
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.         } finally {  
  18.             out.close();  
  19.         }  
  20.     }  

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:


  1. package springmvc.extention;  
  2.   
  3. import java.nio.charset.Charset;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.springframework.beans.BeansException;  
  8. import org.springframework.beans.factory.config.BeanPostProcessor;  
  9. import org.springframework.http.MediaType;  
  10. import org.springframework.http.converter.StringHttpMessageConverter;  
  11.   
  12. /**  
  13.  * Solve the problem of @ResponseBody scrambling in spring MVC3 
  14.  */    
  15. public class UTF8StringBeanPostProcessor implements BeanPostProcessor {    
  16.     @Override    
  17.     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {    
  18.         if (bean instanceof StringHttpMessageConverter) {    
  19.             MediaType mediaType = new MediaType("text""plain", Charset.forName("UTF-8"));    
  20.             List<MediaType> types = new ArrayList<MediaType>();    
  21.             types.add(mediaType);    
  22.             ((StringHttpMessageConverter) bean).setSupportedMediaTypes(types);    
  23.         }    
  24.         return bean;    
  25.     }    
  26.     
  27.     @Override    
  28.     public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {    
  29.         return bean;    
  30.     }    
  31. }  

Then you can register the bean in your Spring configuration file, try your own program, and find that the problem has been solved.

  1. <! - Resolve Chinese scrambling using @ResponseBody. >
  2.     <bean class="springmvc.extention.UTF8StringBeanPostProcessor"></bean>  

(Original address: http://blog.csdn .NET/zhshulin)

Posted by cmpennington on Sun, 24 Mar 2019 11:06:28 -0700