学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
2013-09-18 10:58
666 查看
pring返回json时中文显示乱码的问题,网络上大多数的方法在Spring 3.1下都失效了。搞不懂Spring怎么不修正这个问题呢?
多费周折最终还是找到解决方案,并亲测通过,故分享之。
简单的说就是新建个转换类再注入。就那么简单,这就是开源的好处啊!
配置:
多费周折最终还是找到解决方案,并亲测通过,故分享之。
简单的说就是新建个转换类再注入。就那么简单,这就是开源的好处啊!
配置:
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.abc.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; } } } 另外写好后可能出现一些问题,因为我解决这个问题的时候就出了下面一个问题: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版本需要和你包的版本一致 不然会报错。 网上找了很多方法,这个是我研究后解决的方法,可以用的
相关文章推荐
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案
- springmvc学习笔记-返回json的日期格式问题的解决方法
- springmvc 通过@ResponseBody 返回json的中文乱码解决方案2个
- springmvc中@responseBody返回Json格式中文乱码问题
- springmvc学习笔记--json--返回json的日期格式问题
- springmvc学习笔记--json--返回json的日期格式问题
- 解决springmvc使用ResponseBody注解返回json中文乱码问题
- Java学习笔记37:Spring 使用 @ResponseBody 返回中文乱码问题解决
- springmvc学习笔记--json--返回json的日期格式问题
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
- SpringMVC的@ResponseBody返回JSON,中文乱码问题的解决.
- springMvc框架解决返回Json格式中文乱码问题
- Django学习问题——解决返回JSON数据时中文出现乱码
- 笔记:解决springMVC返回中文乱码问题
- SpringMVC使用注解@ResponseBody返回json以及中文乱码问题解决
- SpringMVC 学习-返回字符串中文乱码问题解决