1.springmvc 3.2以上的版本解决乱码的方法:

  第一步:在配置中加入:

   <mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false" />
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

   第二步:需要的加入的jackson jar如下:

      <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.1.0</version>
</dependency>

2.springmvc 3.1解决中文乱码的方法:

  第一步:在配置文件中加入以下代码:

   <mvc:annotation-driven>

     <mvc:message-converters register-defaults="true">

       <bean class="com.util.spring.UTF8StringHttpMessageConverter"/>

     </mvc:message-converters>

   </mvc:annotation-driven>

第二步:自定义一个编码转换类:

     public class UTF8StringHttpMessageConverter extends
AbstractHttpMessageConverter<String> {   public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
   private final List<Charset> availableCharsets;    public UTF8StringHttpMessageConverter() {
   this(DEFAULT_CHARSET);
   }    public UTF8StringHttpMessageConverter(Charset defaultCharset) {
   super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
   this.availableCharsets = new ArrayList<Charset>(Charset .availableCharsets().values());
   }   @Override
   protected boolean supports(Class<?> clazz) {
   return String.class.equals(clazz);
   }   @Override
   protected String readInternal(Class<? extends String> clazz,
   HttpInputMessage inputMessage) throws IOException,
   HttpMessageNotReadableException {
   MediaType contentType = inputMessage.getHeaders().getContentType();
   Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
  return FileCopyUtils.copyToString(new InputStreamReader(inputMessage .getBody(), charset));
   }   @Override
   protected void writeInternal(String t, HttpOutputMessage outputMessage)
   throws IOException, HttpMessageNotWritableException {
   MediaType contentType = outputMessage.getHeaders().getContentType();
   Charset charset = contentType.getCharSet() != null ? contentType .getCharSet() : DEFAULT_CHARSET;
   FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(), charset));
  }    protected List<Charset> getAcceptedCharsets() {
   return this.availableCharsets;
   }   @Override
  protected Long getContentLength(String s, MediaType contentType) {
   if (contentType != null && contentType.getCharSet() != null) {
  Charset charset = contentType.getCharSet();
   try {
   return (long) s.getBytes(charset.name()).length;
   } catch (UnsupportedEncodingException ex) {
   throw new InternalError(ex.getMessage());
   }
   } else {
   return null;
   }
   }
  }

  第三步:加入需要的jackson包:

      <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.8</version>
</dependency>

最新文章

  1. NOIP2012国王游戏
  2. java 字符串转成 json 数组并且遍历
  3. Linq-分组统计
  4. C# 3个线程A B C 依次打印123123123..
  5. 封装pdo单例模式类
  6. 在windows下使用vs2013编译和调试mysql源代码
  7. GLSL实现Interactive Fluid 流体【转】
  8. [转]Ubuntu下GitHub的使用
  9. UVa839 Not so Mobile
  10. android应用程序ANR定义
  11. 我学的是设计模式的视频教程——命令模式vs策略模式,唠嗑
  12. Spring boot+Thymeleaf+easyui集成:js创建组件页面报错
  13. http 概念
  14. python爬虫-基础入门-python爬虫突破封锁
  15. Opatch使用细则
  16. Python学习之路【第一篇】-Python简介和基础入门
  17. Linux登录超时自动退出处理办法
  18. Thinkphp时间转换与统计的问题
  19. Git实用教程
  20. 设计模式 笔记 状态模式 State

热门文章

  1. Spring中的事务管理详解
  2. [topcoder]BestRoads
  3. [译]GotW #1: Variable Initialization 续
  4. 转:十八、java中this的用法
  5. Activity的启动过程
  6. bzoj1028
  7. Xcode5下去除Icon高光
  8. 【转】 Android——eclipse共享library以及导出jar包
  9. HDOJ/HDU 1180 诡异的楼梯(经典BFS-详解)
  10. jQuery技术内幕电子版4