SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
来源:互联网 发布:淘客发微博用什么软件 编辑:程序博客网 时间:2024/05/21 10:26
多费周折最终还是找到解决方案,并亲测通过,故分享之。
简单的说就是新建个转换类再注入。就那么简单,这就是开源的好处啊!
配置:
- <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;
- }
- }
- }
0 0
- 学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案
- SpringMVC下读取json返回字符串的乱码问题
- springmvc中@responseBody返回Json格式中文乱码问题
- SpringMVC中@ResponseBody注解返回json中文乱码问题
- springMvc框架解决返回Json格式中文乱码问题
- JSON返回的数据中文乱码问题!!!
- springmvc中@responseBody 返回json格式以及中文乱码问题的解决
- 用springmvc作接口时返回json数据中文乱码
- springMVC返回json数据时,字符串中文乱码
- springmvc responseBody返回中文乱码解决方案
- MenuItem 显示中文乱码问题的解决方案
- SpringMVC返回json数据+解决中文乱码
- SpringMVC @ResponseBoby 返回json字符串中文乱码
- SpringMVC 使用@ResponseBody返回json 中文乱码
- redis-哈希数据常规操作
- Unity 按钮动态的绑定和触发方法
- falcon nodata 生成SQL脚本 不用一个个从后来添加
- ASP.NET MVC中Area的另一种用法
- css图片与图片之间总是会存在缝隙
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- Python 实现自动获取种子磁力链接
- Docker Engine 镜像源站
- Handler用法之简单实现轮播图
- 认识jQuery(三书)
- Linux试题1
- try-catch和throw,throws的区别和联系
- CodeMirror源码编辑器
- css中强制换行word-break、word-wrap、white-space区别实例说明