以前使用fastjson替换jackson时,没有直接在页面打印过json,都是js使用没有出现乱码,偶然 打印出来出现了中文乱码

一:之前使用的配置方式,该方式只是使FastJsonHTTPMessageConverter优先级更高,并没有替换Jackson

@Configuration
public class FastJsonConf { @Bean
public HttpMessageConverters fastjsonHttpMessageConverter() {
//消息转换对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//添加fastjson的配置信息,是否格式化返回json
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//转换器中添加配置信息
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
return new HttpMessageConverters(converter);
}
}

二:找到一个博主的文章解决了乱码:http://www.cnblogs.com/xql4j/p/6729524.html

该种方式完全替换了Jackson

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter { /**
* 利用fastjson替换掉jackson,且解决中文乱码问题
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
}

在Springboot 2.0.x以上,使用的是spring5.x版本,该版本移除了WebMvcConfigurerAdapter类,所以上面第二种方式不再使用,如果你的需求是完全替换掉Jackson,应该做新的实现:

常用重写接口:

/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

方式一:

@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer { //WebMvcConfigurer接口空实现 /**
* 完全覆盖Jackson
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
List<MediaType> mediaTypes = Lists.newArrayList();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
converters.add(converter);
}
}

方式二:

@Configuration
class WebMvcConf extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
List<MediaType> mediaTypes = Lists.newArrayList();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
converters.add(converter);
}
}

推荐使用第二种。

如果需要返回多类型,请在list中添加converter

messageConverters.add(new ByteArrayHttpMessageConverter());

messageConverters.add(stringHttpMessageConverter);

messageConverters.add(new ResourceHttpMessageConverter());

messageConverters.add(new ResourceRegionHttpMessageConverter());

messageConverters.add(new SourceHttpMessageConverter<>());

messageConverters.add(new AllEncompassingFormHttpMessageConverter());

2019-08-07:

如果由于Ajax导致的OCTET_STREAM请求后台不支持,在后台添加对应配置即可:
如果你是FastJson配置
List<MediaType> medias = Lists.newArrayList();
medias.add(MediaType.APPLICATION_OCTET_STREAM);
如果你使用的是Jackson,在MappingJackson2HttPMessageConverter的构造方法中可以看到仅仅支持APPLICATION_JSON_UTF_8,
参考以下文章解决:https://www.jianshu.com/p/491ee2df3d02。
场景:上传文件并携带实体,使用@RequestPart注解报错OCTET_STREAM not support
public Result<Object> insert2(@RequestPart(value = "signDO") SignDO signDO, @RequestPart("file") MultipartFile file){}
解决方法以上两种。

springboot使用fastjson中文乱码解决方法 【转载】的更多相关文章

  1. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  2. php mysql 中文乱码解决方法

    本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...

  3. jquery的ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...

  4. Zxing中文乱码解决方法

    Zxing中文乱码解决方法总结 尝试过非常多方法  最后发现此方法解决的乱码最多....... 在百度搜索二维码图片 经过前2页的測试  除开一张图之外  其余都能扫描出结果 假设大家有更好的解决方法 ...

  5. unity3d 中文乱码解决方法——cs代码文件格式批量转化UTF8

    在Unity3d中经常会碰到中文乱码的问题,比如代码中的[AddComponentMenu("GameDef/AI/战机AI")],注释,中文文本等等 其原因在于,unity本身是 ...

  6. Codeblocks中文乱码解决方法

    odeblocks中文乱码解决方法: 特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!! 打开Codeblocks -> 设置 -> 编辑器: 然后点击 Encoding ...

  7. 可遇不可求的Question之导入mysql中文乱码解决方法篇

    可遇不可求的Question之导入mysql中文乱码解决方法篇 先 set names utf8;然后 source c:\1.sql ?

  8. 使用WebLogic时控制台输出中文乱码解决方法

    使用WebLogic时控制台输出中文乱码解决方法 1.找到weblogic安装目录,当前项目配置的domain 2.找到bin下的setDomainEnv.cmd文件 3.打开文件,从文件最后搜索第一 ...

  9. Django 分页查询并返回jsons数据,中文乱码解决方法

    Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...

随机推荐

  1. 高校表白APP-冲刺第一天

    今天我们开了第一次会议, 一.任务: 今日任务布局登录页面,注册页面,修改密码界面 明日任务完成基本的登录页面框架 二.遇到的困难: 布局文件里的一些标签,用法不清楚,页面跳转都得学习.

  2. 流程控制语句(if switch)

    一.if语句 if(条件){ 代码块1 } else if (条件2) { 代码块2 } else if (条件3) { 代码块3 else { 代码块4 } 当代码执行到这里的时候,先判断条件1的值 ...

  3. oracle网络服务之beq协议和SDU优化(性能提升可达30%)

    oracle网络服务之beq协议和SDU优化(性能提升可达30%) 12.3.1  BEQ协议 如果Oracle数据库服务端和客户端在同一台机器上,可以使用BEQ连接,BEQ连接采用进程间直接通信,不 ...

  4. JSOI2020备考知识点复习

    我太菜了qaq,我好爱咕咕咕啊 在NOIP2018爆炸后,我只能指望着在JSOI2019JSOI2020上咸鱼翻身(flag*1) 所以,我要开始复习学习(flag*2) 此博客文会不定时更新qaq( ...

  5. bat实现往hosts文件追加内容

    做个笔记. @echo off ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::: ...

  6. 你不知道的JS(2)深入了解闭包

    很久之前就想写一篇关于闭包的博客了,但是总是担心写的不够完全.不够好,不管怎样,还是要把我理解的闭包和大家分享下,比较长,希望耐心看完. 定义 说实话,给闭包下一个定义是很困难的,原因在于javasc ...

  7. Oracle基础知识点——Oracle服务端和客户端

    Oracle服务端 服务端提供oracle服务的实例,其是数据库的核心,用于数据库的管理,对象的管理与存储.数据的存储.查询.数据库资源的监控.监听等一些服务. 例子:比如一台机子上安装了Oracle ...

  8. linux基础之CentOS7新特性

    CentOS7开机启动顺序: POST --> Boot Sequence --> Bootloader --> kernel + initramfs(initrd) --> ...

  9. Java包装类介绍与类型之间相互转换

    1.包装类存在的意义 通俗解释就是由于Java是面对对象的语言,而基本类型不具有面对对象的概念,为了弥补不足,引入了包装类方便使用面对对象的变成思想操作基本类型. 2.基本类型和包装类对应关系 byt ...

  10. Kde桌面的Mac化

    KDE->Mac \(Mac\) 的审美可以说是很成熟了,确实让人很喜欢啊!于是不由得想弄一个 \(Mac\) 风的桌面. 先放张图吧: 效果还蛮不错的哇. \(Mac\) 原生的壁纸下载链接: ...