SpringMVC 3.1下返回json時中文顯示亂碼問題的解決方案

Spring返回json時中文顯示亂碼的問題,網絡上大多數的方法在Spring 3.1下都失效了。搞不懂Spring怎麼不修正這個問題呢?

多費周折最終還是找到解決方案,並親測通過,故分享之。
簡單的說就是新建個轉換類再注入。就那麼簡單,這就是開源的好處啊!
 
配置:
  1. <mvc:annotation-driven> 
  2.     <mvc:message-converters register-defaults="true"> 
  3.         <bean class="com.abc.spring.UTF8StringHttpMessageConverter"/> 
  4.     </mvc:message-converters> 
  5. </mvc:annotation-driven> 


轉換類:

  1. public class UTF8StringHttpMessageConverter extends 
  2.         AbstractHttpMessageConverter<String> { 
  3.  
  4.     public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
  5.     private final List<Charset> availableCharsets; 
  6.  
  7.     public UTF8StringHttpMessageConverter() { 
  8.         this(DEFAULT_CHARSET); 
  9.     } 
  10.  
  11.     public UTF8StringHttpMessageConverter(Charset defaultCharset) { 
  12.         super(new MediaType("text""plain", defaultCharset), MediaType.ALL); 
  13.         this.availableCharsets = new ArrayList<Charset>(Charset 
  14.                 .availableCharsets().values()); 
  15.     } 
  16.  
  17.     @Override 
  18.     protected boolean supports(Class<?> clazz) { 
  19.         return String.class.equals(clazz); 
  20.     } 
  21.  
  22.     @Override 
  23.     protected String readInternal(Class<? extends String> clazz, 
  24.             HttpInputMessage inputMessage) throws IOException, 
  25.             HttpMessageNotReadableException { 
  26.         MediaType contentType = inputMessage.getHeaders().getContentType(); 
  27.         Charset charset = contentType.getCharSet() != null ? contentType 
  28.                 .getCharSet() : DEFAULT_CHARSET; 
  29.         return FileCopyUtils.copyToString(new InputStreamReader(inputMessage 
  30.                 .getBody(), charset)); 
  31.     } 
  32.  
  33.     @Override 
  34.     protected void writeInternal(String t, HttpOutputMessage outputMessage) 
  35.             throws IOException, HttpMessageNotWritableException { 
  36.         MediaType contentType = outputMessage.getHeaders().getContentType(); 
  37.         Charset charset = contentType.getCharSet() != null ? contentType 
  38.                 .getCharSet() : DEFAULT_CHARSET; 
  39.         FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(), 
  40.                 charset)); 
  41.     } 
  42.  
  43.     protected List<Charset> getAcceptedCharsets() { 
  44.         return this.availableCharsets; 
  45.     } 
  46.      
  47.     @Override 
  48.     protected Long getContentLength(String s, MediaType contentType) { 
  49.         if (contentType != null && contentType.getCharSet() != null) { 
  50.             Charset charset = contentType.getCharSet(); 
  51.             try { 
  52.                 return (long) s.getBytes(charset.name()).length; 
  53.             } catch (UnsupportedEncodingException ex) {                 
  54.                 throw new InternalError(ex.getMessage()); 
  55.             } 
  56.         } else { 
  57.             return null
  58.         } 
  59.     } 


參考:Spring Response Body回傳String產生亂碼

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章