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>

springmvc使用@ResponseBody返回json乱码解决方法的更多相关文章

  1. SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.

    SpringMVC的@ResponseBody,返回json,如果有中文显示乱码的解决办法. 在SpringMVC的配置文件中 <bean class="org.springframe ...

  2. 解决SpringMVC的@ResponseBody返回中文乱码

    SpringMVC的@ResponseBody返回中文乱码的原因是SpringMVC默认处理的字符集是ISO-8859-1,在Spring的org.springframework.http.conve ...

  3. SpringMVC 使用@ResponseBody返回json 中文乱码与返回实体类报错

    有时候我们发现接收的是中文,返回却是个?.这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 /** * Implementation of {@link Htt ...

  4. SpringMVC 使用@ResponseBody返回json 中文乱码

    这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3. ...

  5. SpringMVC参数绑定、Post乱码解决方法

    从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上. springmvc中,接收页面提交的数据是通过方法形参来接收.而不是在control ...

  6. springmvc 通过@ResponseBody 返回json的中文乱码解决方案2个

    1.方法上面的RequestMapping要加上红色的部分.   @ResponseBody     @RequestMapping(value = "/search", prod ...

  7. springMVC使用@ResponseBody返回json

    json格式:{"totle":22,"rows":"sss"} map格式:{totle=22, rows=sss} 一.springMV ...

  8. 处理springMvc中responsebody返回中文乱码

    法一:   @RequestMapping(value="/getUsersByPage",produces = public String getUsers  法二:在sprin ...

  9. ssm 注解@ResponseBody 返回json 乱码问题

    方法一:在@RequestMapping上加入 produces方法 @RequestMapping(value = "/upload.do",method = RequestMe ...

随机推荐

  1. C#学习笔记二:C#程序结构

    从最简单的HelloWorld开始入手,这是一个最低限度的C#程序结构. C# Hello World 示例 一个C#程序主要由以下几部分组成: 命名空间声明 一个类 类方法 类属性 一个Main方法 ...

  2. [转贴]watin的一些例子

    Some WatiN ExamplesBelow are some examples of WatiN codes for testing:// find div by idvar div = bro ...

  3. 【简译】jQuery对象的奥秘:基础介绍

    本文翻译自此文章 你有没有遇到过类似$(".cta").click(function(){})这样的JavaScript代码并且在想“$('#x')是什么”?如果这些对你想天书一样 ...

  4. Android MediaPlayer播放一般音频与SoundPool播放短促的音效

    [1]使用MediaPlayer实现一般的音频播放 MediaPlayer播放通常的音频文件 MediaPlayer mediaPlayer = new MediaPlayer(); if (medi ...

  5. Codevs_1230_元素查找_(set/Hash)

    描述 http://codevs.cn/problem/1230/ ... 1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目 ...

  6. WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞

    漏洞名称: WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞 CNNVD编号: CNNVD-201310-501 发布时间: 201 ...

  7. MySQL 备份表和数据

    方法1: Create table new_table_name (Select * from old_table_name); 方法2: 1.先备份表结构和数据#导出命令 -u用户名 -p密码 -h ...

  8. Remarks on a preprint

    Page 2 Line 1, "reads" should be "read". Page 2 Line 5, "are initial veloci ...

  9. [App]Xamarin学习资料收集

    在博客园其实有很多朋友都在介绍Xamarin的使用方法,下面是比较活跃的一些: http://www.cnblogs.com/yaozhenfa/

  10. Web---自己写的一个简单云相册~

    实现的功能是: 用户可以一次上传一个至多个文件. 用户可以下载其他人上传的图片. 用户可以查看其他所有人的图片. 用户只能删除通过自己IP上传的图片. 用到的技术: 文件上传下载.设计模式.Dom4j ...