Chinaunix首页 | 论坛 | 博客
  • 博客访问: 352870
  • 博文数量: 88
  • 博客积分: 1673
  • 博客等级: 上尉
  • 技术积分: 934
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-20 13:51
文章分类

全部博文(88)

文章存档

2016年(1)

2015年(4)

2014年(3)

2013年(7)

2012年(11)

2011年(1)

2009年(61)

我的朋友

分类: Java

2015-11-06 14:25:05

在spring的json中会出现乱码,是因为项目中使用utf-8,而annotation-driven 默认转码为ISO-8859-1重新定义为UTF-8,直接贴代码,记录下,

点击(此处)折叠或打开

  1. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  2.         <property name="messageConverters">
  3.             <list>
  4.                 <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  5.                     <property name="supportedMediaTypes">
  6.                         <list>
  7.                             <value>text/html;charset=UTF-8</value>
  8.              <value>text/json;charset=UTF-8</value>
  9.              <value>application/json;charset=UTF-8</value>
  10.                         </list>
  11.                     </property>
  12.                 </bean>
  13.             </list>
  14.         </property>
  15.     </bean>


点击(此处)折叠或打开

  1. <mvc:annotation-driven
  2.         content-negotiation-manager="contentNegotiationManager" >
  3.         <mvc:message-converters register-defaults="true">
  4.      <bean class="com.kuanrf.web.core.filter.UTF8StringHttpMessageConverter"/>
  5.      </mvc:message-converters>
  6.     </mvc:annotation-driven>
  7.     <bean id="contentNegotiationManager"
  8.         class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  9.         <property name="mediaTypes">
  10.             <value>
  11.                 html=text/html
  12.                 json=application/json
  13.             </value>
  14.         </property>
  15.         <property name="defaultContentType" value="text/html" />
  16.     </bean>

点击(此处)折叠或打开

  1. package com.kuanrf.web.core.filter;

  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.OutputStreamWriter;
  5. import java.io.UnsupportedEncodingException;
  6. import java.nio.charset.Charset;
  7. import java.util.ArrayList;
  8. import java.util.List;

  9. import org.springframework.http.HttpInputMessage;
  10. import org.springframework.http.HttpOutputMessage;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.http.converter.AbstractHttpMessageConverter;
  13. import org.springframework.http.converter.HttpMessageNotReadableException;
  14. import org.springframework.http.converter.HttpMessageNotWritableException;
  15. import org.springframework.util.FileCopyUtils;

  16. public class UTF8StringHttpMessageConverter extends
  17.         AbstractHttpMessageConverter<String> {

  18.     public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
  19.     private final List<Charset> availableCharsets;

  20.     public UTF8StringHttpMessageConverter() {
  21.         this(DEFAULT_CHARSET);
  22.     }

  23.     public UTF8StringHttpMessageConverter(Charset defaultCharset) {
  24.         super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
  25.         this.availableCharsets = new ArrayList<Charset>(Charset
  26.                 .availableCharsets().values());
  27.     }

  28.     @Override
  29.     protected boolean supports(Class<?> clazz) {
  30.         return String.class.equals(clazz);
  31.     }

  32.     @Override
  33.     protected String readInternal(Class<? extends String> clazz,
  34.             HttpInputMessage inputMessage) throws IOException,
  35.             HttpMessageNotReadableException {
  36.         MediaType contentType = inputMessage.getHeaders().getContentType();
  37.         Charset charset = contentType.getCharSet() != null ? contentType
  38.                 .getCharSet() : DEFAULT_CHARSET;
  39.         return FileCopyUtils.copyToString(new InputStreamReader(inputMessage
  40.                 .getBody(), charset));
  41.     }

  42.     @Override
  43.     protected void writeInternal(String t, HttpOutputMessage outputMessage)
  44.             throws IOException, HttpMessageNotWritableException {
  45.         MediaType contentType = outputMessage.getHeaders().getContentType();
  46.         Charset charset = contentType.getCharSet() != null ? contentType
  47.                 .getCharSet() : DEFAULT_CHARSET;
  48.         FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(),
  49.                 charset));
  50.     }

  51.     protected List<Charset> getAcceptedCharsets() {
  52.         return this.availableCharsets;
  53.     }

  54.     @Override
  55.     protected Long getContentLength(String s, MediaType contentType) {
  56.         if (contentType != null && contentType.getCharSet() != null) {
  57.             Charset charset = contentType.getCharSet();
  58.             try {
  59.                 return (long) s.getBytes(charset.name()).length;
  60.             } catch (UnsupportedEncodingException ex) {
  61.                 throw new InternalError(ex.getMessage());
  62.             }
  63.         } else {
  64.             return null;
  65.         }
  66.     }
  67. }


阅读(1455) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~