spring cloud 学习(11) - 用fastson替换jackson及用gb2312码输出
前几天遇到一个需求,因为要兼容旧项目的编码格式,需要spring-cloud的rest接口,输出gb2312编码,本以为是一个很容易的事情,比如下面这样:
- @RequestMapping(method = RequestMethod.POST, value = "syncPaymentList",
- consumes = {"application/json; charset=gb2312"},
- produces = {"application/json; charset=gb2312"})
- public GatewayDataResult<DcbOrderListResponse> syncPaymentList(SyncPaymentListRequest request) {
- ...return ...;
- }
发现只是把输出的response里Content-Type变成了application/json;charset=gb2312,内容本身并没有变化(即:浏览器设置成简体中文,显示乱码)
有一个很简单粗暴的办法,到是可以(参考下面的),但是对原来代码改变太大:
- @RequestMapping(method = RequestMethod.GET, value = "/test")
- public void gb2312Test(HttpServletResponse response) throws IOException {
- response.setContentType("application/json;charset=gb2312");
- PrintWriter out = response.getWriter();
- out.print("{\"errno\":12,\"errmsg\":\"登录超时\"}");
- return;
- }
另外网有一些办法,比如修改application.yml
- spring:
- http:
- encoding:
- enabled: true
- charset: GB2312
- force: true
相当于传统spring-mvc中下面这段配置
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>gb2312</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </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就可以了:
- @Bean
- public HttpMessageConverters fastJsonHttpMessageConverters() {
- FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
- FastJsonConfig fastJsonConfig = new FastJsonConfig();
- fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
- fastJsonConfig.setCharset(Charset.forName("gb2312"));
- List<MediaType> fastMediaTypes = new ArrayList<>();
- fastMediaTypes.add(MediaType.APPLICATION_JSON);
- fastConverter.setSupportedMediaTypes(fastMediaTypes);
- fastConverter.setFastJsonConfig(fastJsonConfig);
- HttpMessageConverter<?> converter = fastConverter;
- return new HttpMessageConverters(converter);
- }
spring cloud 学习(11) - 用fastson替换jackson及用gb2312码输出的更多相关文章
- spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法
turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...
- Spring Cloud学习(一):Eureka服务注册与发现
1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...
- spring cloud 学习资料
spring cloud 学习资料 网址 拜托!面试请不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA
- Spring Boot和Spring Cloud学习资源推荐
Spring Boot和Spring Cloud学习资源推荐 比较好的学习资源,分享一下. 1.Spring Boot官方文档:http://projects.spring.io/spring-b ...
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- spring cloud 学习之服务消费者(rest+ribbon)
学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...
- 搭建服务与负载均衡的客户端-Spring Cloud学习第二天(非原创)
文章大纲 一.Eureka中的核心概念二.Spring RestTemplate详解三.代码实战服务与负载均衡的客户端四.项目源码与参考资料下载五.参考文章 一.Eureka中的核心概念 1. 服务提 ...
- Spring Cloud 学习 之 Spring Cloud Ribbon(基础知识铺垫)
文章目录 1.负载均衡: 2.RestTemplate详解: xxxForEntity/xxxForObject:主要介绍get跟post exchange: execute源码分析: 1.负载均衡: ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
随机推荐
- 文件上传submit、ajax方式
submit方式: <form id="postForm" name="postForm" action="${rc.contextPath}/ ...
- G - DNA sequence HDU - 1560
题目链接: https://vjudge.net/contest/254151#problem/G AC代码: #include<iostream> #include<cstring ...
- CSS font系列
font-family font-family: Verdana,Helvetica,Arial,"Microsoft YaHei",sans-serif; font-family ...
- 【译】EntityFramework6与EntityFrameworkCore的区别
EntityFramework6 EF6 是一个久经考验的数据库访问技术,发展多年,拥有许多特性,并且成熟稳定.2008年EF作为 .Net 3.5 Sp1 和Visual Studio 2008 S ...
- re模块逐步进阶
Windows 10家庭中文版,Python 3.6.4, 正则表达式,自己一直的水平是 知道,但不熟悉,简单的也能写,复杂的就需要看资料了,距离灵活运用还是差那么一些的. 于是,今天(180831) ...
- TypeError: Object of type 'int64' is not JSON serializable
错误类型:TypeError: Object of type 'int64' is not JSON serializable 错误场景:对Numpy和Pandas结果进行json.dumps报错 错 ...
- FormData介绍
FormData XMLHttpRequest Level 2添加了一个新的接口FormData.利用FormData对象,我们可以通过JavaScript用一些键值对来模拟一系列表单控件,我们还可以 ...
- mongodb分页查询
Limit与Skip方法 MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指 ...
- darknet
darknet第二作者:https://github.com//AlexeyAB/darknet#how-to-train-to-detect-your-custom-objects yolov3实现 ...
- 图学ES6-2.let与const命令