学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案

来源:互联网 发布:用友软件好用吗 编辑:程序博客网 时间:2024/04/30 15:24

pring返回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.     } 
另外写好后可能出现一些问题,因为我解决这个问题的时候就出了下面一个问题:

The prefix "mvc" for element "mvc:annotation-driven" is not bound

解决办法

在xml的beans中添加 

xmlns:mvc="http://www.springframework.org/schema/mvc" 

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

另外上面是spring版本需要和你包的版本一致 不然会报错。
网上找了很多方法,这个是我研究后解决的方法,可以用的