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集 ...
随机推荐
- [转载]使用SQL Server 2008的事务日志传送功能备份数据库(logshiping)
http://www.cnblogs.com/benbenkoala/archive/2009/03/11/1407793.html 引言:SQL Server的事务日志传送备份是仅次于镜像的高可靠性 ...
- 利用JS实现图片的缓存
web页面使用HTML的<img>元素来嵌入图片,和所有HTML元素一样,<img>元素也是可以通过脚本来操控的(设置元素的src属性,将其指向一个新的URL会导致浏览器载入并 ...
- 第7月第19天 swift on linux
1. https://github.com/iachievedit/moreswift http://dev.iachieved.it/iachievedit/more-swift-on-linux/ ...
- 02 uni-app框架学习:设置全局样式统一每个页面的背景颜色
1.设置全局样式可以在App.vue里面 2.在每个页面的根view 里添加一个class名叫page
- TensorFlow 从零到helloWorld
目录 1.git安装与使用 1.1 git安装 1.2 修改git bash默认路径 1.3 git常用操作 2.环境搭建 2.1 tensorflow安装 2.2 CUDA安装 2.3 ...
- 【ARTS】01_06_左耳听风-20181217~1223
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- js 替换任意字符串中间几位为*星号
<script> var str='河南纳智企业管理咨询有限公司'; a=str.substr(0,2)+'***'+str.substr(5,str.split('').length); ...
- idea如何导入一个maven项目
1.import project 2.选择maven项目 3.选择第二个external moudle,选择maven, 4.点击next,一次点击1,2,3,4 5.设置maven环境 6.点击ok ...
- postman发送json请求,使用案例
介绍: postman是一个很好的http模拟器,,可以发送get.post.put等各种请求,是测试服务接口相当好的工具. postman发送json请求,使用案例 发送json的具体步骤: 1. ...
- Demo004 迷宫的生成与遍历的代码审查
1.传送门 合作伙伴: 嚯唶 CnBlogs: 嚯唶 Coding:Rst321 代码: 迷宫的遍历与生成 2. 作业要求 (1). 首先在同学中找一个同伴,范围不限,可以在1~5班中随意组合,建议尽 ...