博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot | 配置fastjson
阅读量:4182 次
发布时间:2019-05-26

本文共 4655 字,大约阅读时间需要 15 分钟。

两种方式配置fastjson

一、在启动类直接注入bean

@Bean    public HttpMessageConverters fastJsonHttpMessageConverters(){        //1. 需要定义一个converter转换消息的对象        FastJsonHttpMessageConverter fasHttpMessageConverter = new FastJsonHttpMessageConverter();        //2. 添加fastjson的配置信息,比如:是否需要格式化返回的json的数据        FastJsonConfig fastJsonConfig = new FastJsonConfig();        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);        //3. 在converter中添加配置信息        fasHttpMessageConverter.setFastJsonConfig(fastJsonConfig);        return new HttpMessageConverters((HttpMessageConverter
) fasHttpMessageConverter); }

二、创建config类实现WebMvcConfigurer(springboot为2.0版本的)

package com.example.config;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import org.springframework.context.annotation.Configuration;import org.springframework.http.MediaType;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.ArrayList;import java.util.List;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/14 12:46 * @description V1.0 */@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {    @Override    public void configureMessageConverters(List
> converters) { converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter); converters.add(fastJsonHttpMessageConverter()); System.out.println("converters = " + converters); } /** * fastJson相关设置 */ private FastJsonHttpMessageConverter fastJsonHttpMessageConverter() { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); List
supportedMediaTypes = new ArrayList
(); supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(getFastJsonConfig()); return fastJsonHttpMessageConverter; } /** * fastJson相关设置 */ private FastJsonConfig getFastJsonConfig() { FastJsonConfig fastJsonConfig = new FastJsonConfig(); // 在serializerFeatureList中添加转换规则 List
serializerFeatureList = new ArrayList
(); serializerFeatureList.add(SerializerFeature.PrettyFormat); serializerFeatureList.add(SerializerFeature.WriteMapNullValue); serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty); serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty); serializerFeatureList.add(SerializerFeature.DisableCircularReferenceDetect); SerializerFeature[] serializerFeatures = serializerFeatureList.toArray(new SerializerFeature[0]); fastJsonConfig.setSerializerFeatures(serializerFeatures); return fastJsonConfig; }}

三、测试

新建个pojo类

package com.example.entity;import com.alibaba.fastjson.annotation.JSONField;import lombok.Data;import java.time.LocalDateTime;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/2/14 10:09 * @description V1.0 */@Datapublic class FastJson {    private int id;    private String name;    @JSONField(format = "yyyy-MM-dd hh:mm:ss")    private LocalDateTime birthDate;}

 

接着创建个控制器类

package com.example.controller;import com.alibaba.fastjson.JSON;import com.example.entity.FastJson;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/2/14 9:59 * @description V1.0 验证项目集成fastjson是否成功  观察下面的demo2方法表明fastjson配置生效了,默认是启用jackjson */@RestController@RequestMapping("fastJson")public class FastJsonController {    @GetMapping("demo2")    public FastJson demo2(){        FastJson fastJson = new FastJson();        fastJson.setId(1);        fastJson.setName("xiaobu");        fastJson.setBirthDate(LocalDateTime.now());        return  fastJson;    }    @GetMapping("demo3")    public String demo3(){        FastJson fastJson = new FastJson();        fastJson.setId(1);        fastJson.setName("xiaobu");        fastJson.setBirthDate(LocalDateTime.now());       return JSON.toJSONString(fastJson);    }}

结果:

 


参考:

转载地址:http://sfgai.baihongyu.com/

你可能感兴趣的文章
SVG学习之——HTML 页面中的 SVG
查看>>
SVG 滤镜学习之——SVG 滤镜
查看>>
mysql中用命令行复制表结构的方法
查看>>
hbase shell出现ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException
查看>>
让代码变得更优雅-Lombok
查看>>
解决Rhythmbox乱码
查看>>
豆瓣爱问共享资料插件发布啦
查看>>
kermit的安装和配置
查看>>
vim 配置
查看>>
openocd zylin
查看>>
linux中cat命令使用详解
查看>>
java中的异常机制
查看>>
商务智能-基本方法-数据钻取
查看>>
C++程序员技术需求规划(发展方向)
查看>>
JNI
查看>>
IOS程序开发框架
查看>>
安装jdk的步骤
查看>>
简述JAVA运算符
查看>>
简易ATM源代码及运行结果
查看>>
简述Java中的简单循环
查看>>