前几天遇到一个需求,因为要兼容旧项目的编码格式,需要spring-cloud的rest接口,输出gb2312编码,本以为是一个很容易的事情,比如下面这样:

  1. @RequestMapping(method = RequestMethod.POST, value = "syncPaymentList",
  2. consumes = {"application/json; charset=gb2312"},
  3. produces = {"application/json; charset=gb2312"})
  4. public GatewayDataResult<DcbOrderListResponse> syncPaymentList(SyncPaymentListRequest request) {
  5. ...return ...;
  6. }

发现只是把输出的response里Content-Type变成了application/json;charset=gb2312,内容本身并没有变化(即:浏览器设置成简体中文,显示乱码)

有一个很简单粗暴的办法,到是可以(参考下面的),但是对原来代码改变太大:

  1. @RequestMapping(method = RequestMethod.GET, value = "/test")
  2. public void gb2312Test(HttpServletResponse response) throws IOException {
  3. response.setContentType("application/json;charset=gb2312");
  4. PrintWriter out = response.getWriter();
  5. out.print("{\"errno\":12,\"errmsg\":\"登录超时\"}");
  6. return;
  7. }

另外网有一些办法,比如修改application.yml

  1. spring:
  2. http:
  3. encoding:
  4. enabled: true
  5. charset: GB2312
  6. force: true  

相当于传统spring-mvc中下面这段配置

  1. <filter>
  2. <filter-name>CharacterEncodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>gb2312</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>forceEncoding</param-name>
  10. <param-value>true</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>CharacterEncodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>

发现也没用,然后看了下jackson2的源码,com.fasterxml.jackson.core.JsonEncoding这个类,默认就只支持UTF-8/16编码,要支持其它编码的话

得自己扩展JsonGenerator,写一堆代码,太复杂,参考:https://stackoverflow.com/questions/10004241/jackson-objectmapper-with-utf-8-encoding

最后想起了以前dubbo中用fastjson替换jackson时,解决过类似问题(参考 dubbox REST服务使用fastjson替换jackson) ,发现了一个很简单的办法,拿fastjson替换jackson2,只要注入下面这个bean就可以了:

  1. @Bean
  2. public HttpMessageConverters fastJsonHttpMessageConverters() {
  3. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  4. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  5. fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
  6. fastJsonConfig.setCharset(Charset.forName("gb2312"));
  7.  
  8. List<MediaType> fastMediaTypes = new ArrayList<>();
  9. fastMediaTypes.add(MediaType.APPLICATION_JSON);
  10. fastConverter.setSupportedMediaTypes(fastMediaTypes);
  11.  
  12. fastConverter.setFastJsonConfig(fastJsonConfig);
  13. HttpMessageConverter<?> converter = fastConverter;
  14. return new HttpMessageConverters(converter);
  15. }

spring cloud 学习(11) - 用fastson替换jackson及用gb2312码输出的更多相关文章

  1. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  2. Spring Cloud学习(一):Eureka服务注册与发现

    1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...

  3. spring cloud 学习资料

    spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA

  4. Spring Boot和Spring Cloud学习资源推荐

    Spring Boot和Spring Cloud学习资源推荐   比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...

  5. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  6. spring cloud 学习之服务消费者(rest+ribbon)

    学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...

  7. 搭建服务与负载均衡的客户端-Spring Cloud学习第二天(非原创)

    文章大纲 一.Eureka中的核心概念二.Spring RestTemplate详解三.代码实战服务与负载均衡的客户端四.项目源码与参考资料下载五.参考文章 一.Eureka中的核心概念 1. 服务提 ...

  8. Spring Cloud 学习 之 Spring Cloud Ribbon(基础知识铺垫)

    文章目录 1.负载均衡: 2.RestTemplate详解: xxxForEntity/xxxForObject:主要介绍get跟post exchange: execute源码分析: 1.负载均衡: ...

  9. Spring Cloud 学习笔记(二)——Netflix

    4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...

随机推荐

  1. 文件上传submit、ajax方式

    submit方式: <form id="postForm" name="postForm" action="${rc.contextPath}/ ...

  2. G - DNA sequence HDU - 1560

    题目链接: https://vjudge.net/contest/254151#problem/G AC代码: #include<iostream> #include<cstring ...

  3. CSS font系列

    font-family font-family: Verdana,Helvetica,Arial,"Microsoft YaHei",sans-serif; font-family ...

  4. 【译】EntityFramework6与EntityFrameworkCore的区别

    EntityFramework6 EF6 是一个久经考验的数据库访问技术,发展多年,拥有许多特性,并且成熟稳定.2008年EF作为 .Net 3.5 Sp1 和Visual Studio 2008 S ...

  5. re模块逐步进阶

    Windows 10家庭中文版,Python 3.6.4, 正则表达式,自己一直的水平是 知道,但不熟悉,简单的也能写,复杂的就需要看资料了,距离灵活运用还是差那么一些的. 于是,今天(180831) ...

  6. TypeError: Object of type 'int64' is not JSON serializable

    错误类型:TypeError: Object of type 'int64' is not JSON serializable 错误场景:对Numpy和Pandas结果进行json.dumps报错 错 ...

  7. FormData介绍

    FormData XMLHttpRequest Level 2添加了一个新的接口FormData.利用FormData对象,我们可以通过JavaScript用一些键值对来模拟一系列表单控件,我们还可以 ...

  8. mongodb分页查询

    Limit与Skip方法 MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指 ...

  9. darknet

    darknet第二作者:https://github.com//AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects yolov3实现 ...

  10. 图学ES6-2.let与const命令