SpringMVC ajax JSON 中文乱码问题
2013-11-02 12:22
399 查看
转载自:http://xyly624.blog.51cto.com/842520/893606
http://blog.csdn.net/dyllove98/article/details/9631553
由于之前一直都是研发自己的MVC和动态表单架构,最近因为一个技术研究的项目,开始接触Spring MVC。各种标注带来了很好的开发感受,但是今天被AJAX的json返回折磨了好久。一直都是乱码。尝试了网上的各种方法,都不理想。
最后通过研究StringHttpMessageConverter源代码发现,开发者很坑的使用了"ISO-8859-1"作为默认编码。这种西欧编码在如今的开源届也不多见,不知道是不是Spring一直忘记改了。
发现这段之后,就好处理了,由于DEFAULT_CHARSET无法通过IoC进行修改,最直接的方式就是完成自己的AbstractHttpMessageConverter,命名为:UTF8StringHttpMessageConverter。逻辑和StringHttpMessageConverter
一致,只需将DEFAULT_CHARSET修改为UTF-8即可。
spring-servlet 配置
controller中
转换类:
http://blog.csdn.net/dyllove98/article/details/9631553
由于之前一直都是研发自己的MVC和动态表单架构,最近因为一个技术研究的项目,开始接触Spring MVC。各种标注带来了很好的开发感受,但是今天被AJAX的json返回折磨了好久。一直都是乱码。尝试了网上的各种方法,都不理想。
最后通过研究StringHttpMessageConverter源代码发现,开发者很坑的使用了"ISO-8859-1"作为默认编码。这种西欧编码在如今的开源届也不多见,不知道是不是Spring一直忘记改了。
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1"); private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return DEFAULT_CHARSET; } }
发现这段之后,就好处理了,由于DEFAULT_CHARSET无法通过IoC进行修改,最直接的方式就是完成自己的AbstractHttpMessageConverter,命名为:UTF8StringHttpMessageConverter。逻辑和StringHttpMessageConverter
一致,只需将DEFAULT_CHARSET修改为UTF-8即可。
spring-servlet 配置
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.abc.spring.UTF8StringHttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>
controller中
@RequestMapping(value = "/getWeather",method = {RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=UTF-8") @ResponseBody
转换类:
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; } } }
相关文章推荐
- 查看jdk的位数
- 加快java开发速度的几个建议
- java输出数组中出现的次数最多的那个及次数
- 枚举实现单例
- Java网上学习资料
- Java synchronized详解
- java.lang.instrument学习 (二),javassist实例练习
- 字符串完美度 ——Java
- java入门程序Hello
- hadoop1.2.1集群安装(虚拟机和主机ubuntu12.04构成)----连接eclipse
- [转载]2014年10月26完美世界校招两道java题
- ubuntu 13.10 eclipse
- .classpath文件的内容(MyEclipse通过添加External Jar生成)
- struts2中BaseAction的模板
- 用java获取当前时间以及怎么设置时间的格式
- SpringMvc环境搭建(下载所需的包)
- java的主方法为什么定义成main
- java参数传递内存图解
- 浅谈Java与C#的一些细微差别
- 介绍下javabean