spring-boot自定义fastjson-starter

1.前言

我们用spring boot发现会有很多spring-boot-starter-xxx,在实际开发中可能会通过@Bean注解来实现组件的加载,我们希望通过配置文件如application.yml或者application.properties来实现自定义类的加载到spring 容器中去。
本文采用spring boot版本为2.1.1为例。
官方文档建议我们自定义组件命名最好采用xxx-spring-boot-starter 命名规则参考

2.准备知识

知识点 学习网址 特性
@Configuration 配置元数据 spring 3.0开始
引入该注解,
@Configuration作用
在用于描述类、
接口(包括注解类型) 或enum声明。
初始化配置类我们知道有2种方式:
1.通过META-INF/
spring.factories文件来实现
org.springframework.boot
.autoconfigure.EnableAuto
Configuration
=
\com.even.fastjson.
FastJsonAutoConfiguration
(spring boot采用SPI的约定)。
2.采用扫包的方式
scanBasePackages实现。
@EnableConfiguration
Properties
注册的属性类 该注解是用来开启对
@ConfigurationProperties注解配置Bean的支持。
也就是@EnableConfiguration
Properties注解
告诉Spring Boot 能支持@ConfigurationProperties。
@ConditionalOnClass 条件注解 含义是有某个类bean才会加载。
@ConditionalOnProperty @Conditional
OnProperty
来控制Configuration
是否生效
通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值。
如果该值为空,则返回false;如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。
如果返回值为false,则该configuration不生效;为true则生效。
@Conditional
OnWebApplication
Web应用程序条件 让配置取决于
应用程序是否是一个“Web应用程序”被包括在内。
Web应用程序是使用
Spring WebApplicationContext,
定义session范围或具有的任何应用程序StandardServletEnvironment。
fastJson fastJson FastJson是一个Java库,可用于将Java对象转换为其JSON表示。 它还可用于将JSON字符串转换为等效的Java对象。
FastJson可以处理任意Java对象,包括您没有源代码的预先存在的对象。

3.依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>${fast.json}</version>
    </dependency>

4.代码实现

1.创建FastJsonProperties类,主要是放默认的fastJson配置文件。

@ConditionalOnClass({JSON.class})
@ConfigurationProperties(prefix = "spring.fastjson")
public class FastJsonProperties {
    /**
     * 默认Content-Type
     */
    private final static String DEFAULT_MEDIA_TYPE = "application/json;charset=UTF-8";
    /**
     * 是否生效
     */
    private boolean enabled;
    /**
     * 默认List<MediaType>
     */
    private List<MediaType> supportedMediaTypes = MediaType.parseMediaTypes(DEFAULT_MEDIA_TYPE);
    /**
     * 实例化一个FastJsonConfig对象
     */
    private FastJsonConfig config = new FastJsonConfig();

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public List<MediaType> getSupportedMediaTypes() {
        return supportedMediaTypes;
    }

    public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
        this.supportedMediaTypes = supportedMediaTypes;
    }

    public FastJsonConfig getConfig() {
        return config;
    }

    public void setConfig(FastJsonConfig config) {
        this.config = config;
    }

2.创建FastJsonAutoConfiguration类。

  • (1)注入配置文件类。
  • (2)过滤配置文件值判断是否生效。
  • (3)重新构造HttpMessageConverter方法。
  • (4)把bean注入到容器中。
@Configuration
@EnableConfigurationProperties({FastJsonProperties.class})
@ConditionalOnClass({JSON.class, HttpMessageConverter.class})
public class FastJsonAutoConfiguration {
    @Resource
    private FastJsonProperties fastJsonProperties;
    @Bean
    @ConditionalOnProperty(prefix = "spring.fastjson", name = "enabled", havingValue = "true", matchIfMissing = true)
    @ConditionalOnWebApplication
    public HttpMessageConverter<?> httpMessageConverter() {
        /**
         * FastJsonHttpMessageConverter转换类
         */
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        /*媒体类型*/
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastJsonProperties.getSupportedMediaTypes());
        /*配置*/
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonProperties.getConfig());
        return fastJsonHttpMessageConverter;
    }

3.在resources下的META-INF里创建spring.factories文件,spring boot通个这个文件来扫包把我们的bean加载到容器中

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.even.fastjson.FastJsonAutoConfiguration

4.为了让IDE编辑器能提示我们一些配置提示我们自己创建一个spring-configuration-metadata.json文件里面添加一些元数据。
参考文档

{
  "properties": [
    {
      "name": "spring.fastjson.supportedMediaTypes",
      "type": "java.lang.String",
      "description": "Description for spring.fastjson.supportedMediaTypes.",
      "defaultValue": "application/json;charset=UTF-8"
    },
    {
      "name": "spring.fastjson.config.serializerFeatures",
      "type": "com.alibaba.fastjson.serializer.SerializerFeature",
      "description": "Description for spring.fastjson.config.serializerFeatures."
    },
    {
      "name": "spring.fastjson.enabled",
      "type": "java.lang.Boolean",
      "description": "Whether to enable FastJsonHttpMessageConverter.",
      "defaultValue": true
    },
    {
      "name": "spring.fastjson.config.dateFormat",
      "type": "java.lang.String",
      "description": "Date serialization format."
    }
  ],
  "hints": [
    {
      "name": "spring.fastjson.supportedMediaTypes",
      "values": [
        {
          "value": "application/json;charset=UTF-8"
        },
        {
          "value": "text/html"
        },
        {
          "value": "text/plain"
        },
        {
          "value": "text/xml"
        }
      ]
    },
    {
      "name": "spring.fastjson.config.serializerFeatures",
      "providers": [
        {
          "name": "handle-as",
          "parameters": {
            "target": "com.alibaba.fastjson.serializer.SerializerFeature"
          }
        }
      ]
    },
    {
      "name": "spring.fastjson.enabled",
      "values": [
        {
          "value": true
        },
        {
          "value": false
        }
      ]
    },
    {
      "name": "spring.fastjson.config.dateFormat",
      "values": [
        {
          "value": "yyyy-MM-dd"
        },
        {
          "value": "yyyy-MM-dd HH:mm:ss"
        },
        {
          "value": "HH:mm:ss"
        }
      ]
    }
  ]
}

6.自定义starter代码完成了需要打成jar包,别的spring-boot只需要将这个包引用进来。
注意打包的时候看一下target/classes/META-INF/下是否有spring.factories文件,我打包的时候出现了没有这个文件导致我测试fastjson一直不生效。
7.测试

  • (1)创建一个新的spring-boot项目。
  • (2)引入spring-boot-fastjson-starter包。
  • (3)在配置文件开启spring.fastjson.enabled=true。
  • (4)写一个demo这里就省略demo。
    User类
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
@Builder
public class User {
    //去掉ID
    @JSONField(serialize=false)
    private Long id;
    @JSONField(ordinal = 3)
    private String name;
    private int age;
    @JSONField(ordinal = 1)
    private LocalDate birthday;
}

(5)输出结果:{"age":20,"birthday":"2018-12-21","name":"even"}

5.资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容