阿超
>
编写一个spring-boot-starter-fastjson
酷烈之祸,多起于玩忽之人;盛满之功,常败于细微之事。——《菜根谭》
这个starter
是自己写的,主要就是注入了个FastJsonConfig
写法如下:首先引入gav
,完整pom.xml
如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.ruben</groupId> <artifactId>spring-boot-starter-fastjson</artifactId> <version>1.0</version>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> <fastjson-version>1.2.79</fastjson-version> <lombok-version>1.18.22</lombok-version> <hutool-version>5.7.22</hutool-version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok-version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson-version}</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-bom</artifactId> <version>${hutool-version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
</project>
|
然后是编写Configuration
和Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package com.ruben.config;
import java.time.LocalDate; import java.time.LocalDateTime;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean;
import com.alibaba.fastjson.parser.ParserConfig; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig;
import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.Opt;
@EnableConfigurationProperties(FastJsonProperties.class) public class FastJsonAutoConfiguration {
@Bean @ConditionalOnMissingBean public FastJsonConfig fastJsonConfig(FastJsonProperties fastJsonProperties) { FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteEnumUsingToString); SerializeConfig serializeConfig = SerializeConfig.globalInstance; fastJsonConfig.setDateFormat(fastJsonProperties.getDatePattern()); serializeConfig.put(LocalDateTime.class, (serializer, object, fieldName, fieldType, features) -> Opt.ofNullable(object).ifPresentOrElse(obj -> serializer.out.writeString( LocalDateTimeUtil.format((LocalDateTime) obj, fastJsonProperties.getLocalDateTimePattern()) ), serializer.out::writeNull)); serializeConfig.put(LocalDate.class, (serializer, object, fieldName, fieldType, features) -> Opt.ofNullable(object).ifPresentOrElse(obj -> serializer.out.writeString( LocalDateTimeUtil.format((LocalDate) obj, fastJsonProperties.getLocalDatePattern()) ), serializer.out::writeNull)); fastJsonConfig.setSerializeConfig(serializeConfig); ParserConfig parserConfig = ParserConfig.getGlobalInstance(); fastJsonConfig.setParserConfig(parserConfig); return fastJsonConfig; }
}
|
Properties
这里简单用于设定时间日期格式化的表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.ruben.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import cn.hutool.core.date.DatePattern; import lombok.Data;
@Data @ConfigurationProperties(FastJsonProperties.PREFIX) public class FastJsonProperties {
public static final String PREFIX = "fastjson";
private String datePattern = DatePattern.NORM_DATETIME_PATTERN; private String localDateTimePattern = DatePattern.NORM_DATETIME_PATTERN; private String localDatePattern = DatePattern.NORM_DATE_PATTERN;
}
|
然后别忘了在resources
下配置
目录结构如下:
1 2 3 4 5 6
| src └ main └ java └ resources └ META-INF └ spring.factories
|
目录对了一般会有图标提示
spring.factories
里面指定我们的配置类
1 2
| org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ruben.config.FastJsonAutoConfiguration
|
我们可以尝试使用一下,首先用maven
进行install
然后到另一个项目中使用
引入GAV
1 2 3 4 5
| <dependency> <groupId>com.ruben</groupId> <artifactId>spring-boot-starter-fastjson</artifactId> <version>1.0</version> </dependency>
|
配置文件这边其实已经可以看到提示了
我们配置其中一个
1 2
| fastjson: date-pattern: yyyy年MM月dd日
|
测试一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.ruben.simplescaffold;
import javax.annotation.Resource;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;
import com.alibaba.fastjson.support.config.FastJsonConfig;
@SpringBootTest class SimpleScaffoldApplicationTests {
@Resource private FastJsonConfig fastJsonConfig;
@Test void testFastJsonConfig() { Assertions.assertEquals(fastJsonConfig.getDateFormat(), "yyyy年MM月dd日"); }
}
|
成功~
starter
的完整代码我放到了这里:https://gitee.com/VampireAchao/spring-boot-starter-fastjson.git