【springboot进阶】基于starter项目构建(二)构建starter项目-fastjson

导读:本篇文章讲解 【springboot进阶】基于starter项目构建(二)构建starter项目-fastjson,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

一、创建 fastjson-spring-boot-starter 项目

二、添加 pom 文件依赖

三、构建配置

四、加载自动化配置

五、打包

六、使用


这个系列讲解项目的构建方式,主要使用 父项目 parent 和 自定义 starter 结合。项目使用最新的 springboot3 和 jdk19。本系列的代码仓库看查看 gitee 仓库 的 starter 目录。

这篇我们开始学习创建属于自己的 starter ,实现一些常用模块的封装和自动配置,模拟 spirngboot 的 starter 模式,看看怎么将项目构建为 fastjson starter

一、创建 fastjson-spring-boot-starter 项目

一般官方的 starter 是以 spring-boot-starter-{模块名},所以我们这边自定义的时候,区分于官方的命令,将模块名放在前面。

我们还是以一个 springboot 项目的方式来创建,如下图。

【springboot进阶】基于starter项目构建(二)构建starter项目-fastjson

选择目前最新的3.0.0版本,下面的依赖不需要勾选,等下我们再添加。

二、添加 pom 文件依赖

先贴上 pom.xml 代码,这里使用到上一章介绍的 backend-parent 父级项目作为这里的 parent

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.liurb.springboot.scaffold</groupId>
        <artifactId>backend-parent</artifactId>
        <version>1.0.0</version>
        <relativePath />
    </parent>

    <artifactId>fastjson-spring-boot-starter</artifactId>
    <version>1.0.0</version>
    <name>fastjson-spring-boot-starter</name>
    <description>fastjson-spring-boot-starter</description>

    <properties>
        <common-spring-boot-starter.version>1.0.0</common-spring-boot-starter.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <optional>true</optional>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
            <optional>true</optional>
            <exclusions>
                <exclusion>
                    <artifactId>tomcat-embed-el</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.liurb.springboot.scaffold</groupId>
            <artifactId>common-spring-boot-starter</artifactId>
            <version>${common-spring-boot-starter.version}</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2-extension</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

依赖说明:

1)fastjson2:使用最新的fastjson2.x版本

2)fastjson2-extension:主要提供消息转化器 FastJsonHttpMessageConverter 

插件说明:

加入 “<classifier>exec</classifier>”,否则其他项目引入后会提示包内的类找不到。

三、构建配置

搭建好的 starter 目录与代码如下图。

【springboot进阶】基于starter项目构建(二)构建starter项目-fastjson

1. FastJsonParser 注解

因为使用到 fastjson 作为消息解析器,所以会碰到一些参数解析方面的场景问题,所以这里定义了一个注解配合 FastJsonParserArgumentResolver 消息解析器 使用。具体的使用方式,可以查看之前的文章 传送门

2. webmvc的配置类 FastjsonWebMvcConfig 

@AutoConfiguration
@ConditionalOnClass({FastJsonParserArgumentResolver.class})
@ConditionalOnProperty(
        name = "web.starter.http-message.config.converter",
        havingValue = "fastjson"
)
public class FastjsonWebMvcConfig extends WebMvcConfigurationSupport {
    
    //todo ...

}

这里有一个配置开关,通过 @ConditionalOnProperty 解析,我们可以在 web starter 项目(后面会说到) 通过配置文件设置 web 项目将使用哪种消息解析器 ,后面会介绍到使用 gson 作为消息解析器。

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new FastJsonParserArgumentResolver());
    }

这里是添加我们的自定义的参数解析器。

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        //字符串转换
        StringHttpMessageConverter strConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converters.add(strConverter);

        //定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
        //添加fastjson的配置信息,比如是否要格式化返回的json数据;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setWriterFeatures(
                //是否输出值为null的字段,默认为false
                JSONWriter.Feature.WriteMapNullValue,
                //将Collection类型字段的字段空值输出为[]
                JSONWriter.Feature.WriteNullListAsEmpty,
                //将字符串类型字段的空值输出为空字符串
                JSONWriter.Feature.WriteNullStringAsEmpty
        );
        //在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //设置支持的媒体类型
        fastConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
        //设置默认字符集
        fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
        //将convert添加到converters
        converters.add(fastConverter);

    }

StringHttpMessageConverter 是解决返回字符串时会带上双引号的问题。

3. 自定义序列化 LocalDateTimeJsonSerializer

添加一些场景内会碰到序列化类,例如这里的 LocalDateTimeJsonSerializer ,就是可以将数据库返回的 LocalDateTime 日期,转换为毫秒数返回给前端。

4. 其他

我们还可以添加一些关于 fastjson 的东西在这个 starter 内,这样其他的项目引用的时候就可以使用这里面的类和配置。

四、加载自动化配置

从 springboot 2.7 的时候,spring.factories 这种方式已经标记为过期的,所以从 springboot3 开始已经完全移除了。所以我们要创建 org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件。

【springboot进阶】基于starter项目构建(二)构建starter项目-fastjson

将我们 conf 目录下的 FastjsonWebMvcConfig 类加包路径定义在这里。如果有多个的情况,就一行配置一个。

五、打包

这时候执行 mvn package & mvn install ,这样就将这个 starter 安装到本地仓库中。

六、使用

可以看 gitee 仓库的 springboot-advance-demo 项目。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/93592.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!