(二)初识springboot之使用fastjson解析数据(解决乱码及简单配置)
2018-02-21 17:22
1121 查看
写在前面的
在我们的项目的开发中,根据不同的需求有时候需要从后台返回json数据。在Spring中常用的返回json数据的两种方法是:(1)使用@RestController在控制器的类上注解,使用该注解的类中所有配置的url映射的方法返回值不进行视图解析,只进行数据解析。(2)使用@Controller在控制器类上注解,在方法上使用@ResponseBody注解,使用该方法的只有在带有@ResponseBody注解的配置url映射的方法返回值不进行视图解析。在springboot中如果不自定义json解析工具,默认使用的是jackson,但在我们日常开发中fastjson使用的比较多,所以本次是介绍如何自定义json解析器
一、在自己的工程中添加fastjson依赖
在pom.xml添加如下依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.38</version> </dependency>
二、编写代码,自定义json解析器
使用更换json解析器有两种方法:(1)启动类继承WebMvcConfigurerAdapter重写configureMessageConverters方法
完整代码如下:
@SpringBootApplication public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args){ SpringApplication.run(Application.class,args); } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters){ super.configureMessageConverters(converters); //定义消息转换器 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //设置配置信息 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //将配置信息添加到转换器中 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); //将自定义的转换器添加到转换器列表中 converters.add(fastJsonHttpMessageConverter); } }
(1)使用@Bean注入消息转换器
完整代码如下:
@Bean public HttpMessageConverters fastJsonHttpMessageConverter() { //定义消息转换器 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //设置配置信息 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //将配置信息添加到转换器中 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); //将自定义的转换器添加到转换器列表中 return new HttpMessageConverters(fastJsonHttpMessageConverter); }
使用加入以上代码后程序就会使用fastjson返回的数据
三、解决中文乱码
使用fastjson解析是出现中文乱码有两种解决方式(1)在转换器中设置字符集
List<MediaType> fastMediaTypes = new ArrayList<MediaType>(); fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
你可以点开MediaType中源码看到,其实就是设置了字符集
//第37行 public static final MediaType APPLICATION_JSON_UTF8 = valueOf("application/json;charset=UTF-8");
(2)设置response的字符集
在配置文件中加入:
spring: http: encoding: charset: UTF-8 enabled: true # force是将request和response都设置字符集 force: true # force单独设置request和response # force-response: true # force-request: false
本人使用的是yml的配置所以多行的,一般项目中建议两个地方都设置统一字符集,比较安全保险
有的朋友提出要设置:
fastJsonConfig.setCharset(Charset.forName(“UTF-8”));
其实这一句可以不用,你查看源码发现,他默认的配置就是UTF-8,所以可以不写的。
四、数据格式简单配置
1、数据解析样式配置可以设置fastJsonConfig.setSerializerFeatures( SerializerFeature.PrettyFormat,//结果是否格式化默认false SerializerFeature.WriteClassName,//输出类名字默认false );
如果你需要更多格式可以查看枚举类SerializerFeature中的定义
2、另外还可以使用ValueFilter进行数据过滤,例如日期过滤
ValueFilter valueFilter = new ValueFilter() { public Object process(Object o, String key, Object value) { if(null!=value){ if(value instanceof Date){ value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(value); } } return value; } }; fastJsonConfig.setSerializeFilters(valueFilter);
更多需求都可以自己设计自定义。
当然使用注解@JSONField(format = “yyyy-MM-dd”)也可定义日期格式,但是使用注解只有注解的属性生效,对于太多相同设置使用这种方式更加便捷
原文地址(我的个人网站):http://te-amo.site/user/article/info/ARTICLE20180221051845138
a05c
相关文章推荐
- spring boot新手教程之使用FastJson解析JSON数据以及解决返回中文乱码问题
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Springboot使用FastJson后,接口返回中文乱码的问题解决。
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- SpringBoot学习:Spring Boot使用FastJson解析JSON数据
- spring boot 使用fastjson 处理json数据中文乱码 默认使用 ISO-8859-1编码格式
- 解决Springboot使用FastJson 返回中文乱码的问题
- Spring Boot使用FastJson解析JSON数据的方法
- Spring Boot 学习4--完美使用FastJson解析Json数据
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- spring boot使用fastJson解析数据
- Spring Boot 完美使用FastJson解析Json数据
- 【Spring Boot】完美使用FastJson解析JSON数据
- 【传智播客郑州校区分享】Spring Boot中使用FastJson解析Json数据
- Spring Boot完美使用FastJson解析JSON数据(十三)
- Spring Boot使用FastJson解析JSON数据
- spring boot 配置fastjson解析json
- 【Spring-boot 】FastJson对json数据进行解析(类型转换)
- SpringBoot 用fastjson替换到jackjson并解决中文乱码的问题