SpringBoot更改HttpMessageConverters使用FastJson出现乱码问题
1、出现问题的现象!如下截图,使用SpringBoot 进行开发,接口返回的内容出现中文乱码?
接口内容想要返回的内容:
页面返回内容:
惊喜不?意外不?
为什么出现这个情况?不例外的话,很多同事都是替换了SpringBoot自带的Json框架为FastJson解析工具了。
在替换的过程中,没有注意编码格式造成的!
@SpringBootApplication(scanBasePackages = {"com.spring.resource.cloud*"})
@ServletComponentScan({"com.spring.resource.cloud*"})
public class ResourceUploadGuestApplication {
public static void main(String[] args) {
SpringApplication.run(ResourceUploadGuestApplication.class, args);
}
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//创建FastJson信息转换对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//创建Fastjosn对象并设定序列化规则
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//规则赋予转换对象
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
return new HttpMessageConverters(fastJsonHttpMessageConverter);
}
}
2、解决问题呗!
我们从上面的代码可以看出,在进行数据转换的时候,直接食用FastJson进行替换了原本的默认转换工具。那既然出现问题,一定是新的转换工具出现了问题!
那我们在设定转换过程,是不是可以设定具体转换之后的数据类型及编码格式呢?答案是肯定的!
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
//创建FastJson信息转换对象
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //创建Fastjosn对象并设定序列化规则
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 中文乱码解决方案
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//设定json格式且编码为UTF-8
fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); //规则赋予转换对象
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); }
这样就解决了乱码问题了!
3、为什么这么修改呢?
如果你看到结果之后,想知道为啥这么修改的话,debug!
初始化的时候,我们看到
SupportedMediaTypes值为 */* 这样对于很多浏览器是识别不了具体的格式和编码类型的,所以出现乱码和非格式化的样子!
(2)指定格式个编码类型之后,出现了JSON格式和UTF-8编码格式,其实对应枚举对象就是
/**
* Public constant media type for {@code application/json;charset=UTF-8}.
*/
public final static MediaType APPLICATION_JSON_UTF8;
小白看问题,浅显不深究
如若表达不清晰或存疑,可留言指教!
感谢来过
放松一下啦,找找下图几个方脸吧!
————————————————————————————————————————————————
(^ _ ^) (^ _ ^)(^ _ ^)(^ _ ^)(^ _ ^)(^ _ ^)
(^ _ ^)[^ _ ^](^ _ ^)(^ _ ^)[^ _ ^](^ _ ^)
(^ _ ^)(^ _ ^)(^ _ ^)(^ _ ^)(^ _ ^)
(^ _ ^)[^ _ ^](^ _ ^)(^ _ ^)
(^ _ ^)(^ _ ^)(^ _ ^)
(^ _ ^)[^ _ ^]
(^ _ ^)
————————————————————————————————————————————————
SpringBoot更改HttpMessageConverters使用FastJson出现乱码问题的更多相关文章
- 如何彻底解决MySQL更改默认字符集以及字符乱码问题!!!
在我们使用MySQL数据库时,字符乱码,对我们来说是一个很头疼的问题.今天笔者就来教大家如何彻底解决更改默认字符集以及字符乱码问题. 当我们使用压缩包进行MySQL安装后,系统会使用默认的字符集,这时 ...
- springboot使用fastjson中文乱码解决方法 【转载】
以前使用fastjson替换jackson时,没有直接在页面打印过json,都是js使用没有出现乱码,偶然 打印出来出现了中文乱码 之前使用的配置方式 @Configuration public cl ...
- Springboot使用alibaba的fastJson,@JSONField不起作用的问题
在Springboot中默认的JSON解析框架是jackson 今天引入alibaba的fastjson,使用@JSONField(serialize=false),让@RestController转 ...
- FastJson中文乱码
初学springboot使用fastJson替换默认的jackson后出现中文乱码 解决方式1: import java.util.ArrayList; import java.util.List; ...
- springboot整合redis,并解决乱码问题。
热烈推荐:超多IT资源,尽在798资源网 springboot 版本为 1.5.9 //如果是2.x 修改 pom.xml 也可切换成 1.5.9 <parent> <groupId ...
- springboot升级2.0 fastjson报错? 2.0以上应该怎么整合fastjson?
SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将JackSon换成 ...
- SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法
楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家: package com.imooc.config; import or ...
- 使用spring-boot 国际化配置所碰到的乱码问题
写好html静态页面 , 也加上了编码格式 , 获取国际化展示在浏览器中还是存在乱码 , 开始以为是浏览器编码格式问题 , 做过处理后任没有得到解决 , 具体的处理方案如下: <meta ht ...
- SpringBoot整合Redis在可视化工具乱码问题,以及常用的api
pom依赖: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
随机推荐
- java 集合框架(一)概述
一.概述 Java Collection Framework (JCF) 提供给我们一系列的类和接口,方便开发者处理集合对象. 在Java 2之前,Java是没有完整的集合框架的.它只有一些简单的可以 ...
- spring schedule定时任务(一):注解的方式
我所知道的java定时任务的几种常用方式: 1.spring schedule注解的方式: 2.spring schedule配置文件的方式: 3.java类继承TimerTask: 第一种方式的实现 ...
- Java之List排序出错
Java之List排序出错 Bound mismatch: The generic method sort(List<T>) of type Collections is not appl ...
- java.sql.SQLException之数组越界
java.sql.SQLException之数组越界 1.具体错误如下: (1)java.sql.SQLException:Parameter index out of range(0<1) ( ...
- zTree实现地市县三级级联Service接口测试
zTree实现地市县三级级联Service接口测试 ProvinceServiceTest.java: /** * @Title:ProvinceServiceTest.java * @Package ...
- Linux显示服务器完整的状态信息
Linux显示服务器完整的状态信息 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ apachectl [fullstatus] Usage: /usr/sbi ...
- html基础加强2
1.表单元素 表单元素一定要提交在form标签里面 2.id和name 所有元素都有ID,但只有表单元素才有name ID不可以重复,name可以重复 name属性的目的时可以将数据提交到服务器.id ...
- Spring有什么缺点?
Spring有什么缺点如下: 1.jsp中要写很多代码: 2.控制器过于灵活,缺少一个公用控制器: 3.不支持分布式部署.
- [USACO15FEB]审查(黄金)Censoring (Gold)
题面 传送门 Sol AC自动机+栈,每次匹配到栈顶减去这个单词的长度,回到之前的状态 最后栈中留下的就是答案 # include <bits/stdc++.h> # define IL ...
- [BZOJ2048] [2009国家集训队] 书堆
Description Input 第一行正整数 N M Output 一行(有换行符),L,表示水平延伸最远的整数距离 (不大于答案的最大整数) Sample Input #11 100 #22 1 ...