Commit ab800aa891829422f814683cb4fe77ba057a8d16
1 parent
50371b36
1. 简化 JsonUtils 初始化方式,去掉 BeanPostProcessor
2. 减少日志打印 目的:优化启动速度
Showing
5 changed files
with
27 additions
and
35 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/json/JsonUtils.java
| ... | ... | @@ -10,7 +10,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | 10 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 11 | 11 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 12 | 12 | import lombok.SneakyThrows; |
| 13 | -import lombok.experimental.UtilityClass; | |
| 14 | 13 | import lombok.extern.slf4j.Slf4j; |
| 15 | 14 | |
| 16 | 15 | import java.io.IOException; |
| ... | ... | @@ -23,7 +22,6 @@ import java.util.List; |
| 23 | 22 | * |
| 24 | 23 | * @author 芋道源码 |
| 25 | 24 | */ |
| 26 | -@UtilityClass | |
| 27 | 25 | @Slf4j |
| 28 | 26 | public class JsonUtils { |
| 29 | 27 | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/AbstractSmsClient.java
yudao-framework/yudao-spring-boot-starter-file/src/main/java/cn/iocoder/yudao/framework/file/core/client/AbstractFileClient.java
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/jackson/config/YudaoJacksonAutoConfiguration.java
| 1 | 1 | package cn.iocoder.yudao.framework.jackson.config; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer; | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 4 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 4 | 5 | import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeDeserializer; |
| 5 | 6 | import cn.iocoder.yudao.framework.jackson.core.databind.LocalDateTimeSerializer; |
| 6 | -import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | |
| 7 | +import cn.iocoder.yudao.framework.jackson.core.databind.NumberSerializer; | |
| 7 | 8 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | 9 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 9 | 10 | import lombok.extern.slf4j.Slf4j; |
| 10 | -import org.springframework.beans.BeansException; | |
| 11 | -import org.springframework.beans.factory.config.BeanPostProcessor; | |
| 12 | 11 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 13 | 12 | import org.springframework.context.annotation.Bean; |
| 14 | 13 | |
| 15 | 14 | import java.time.LocalDateTime; |
| 15 | +import java.util.List; | |
| 16 | 16 | |
| 17 | 17 | @AutoConfiguration |
| 18 | 18 | @Slf4j |
| 19 | 19 | public class YudaoJacksonAutoConfiguration { |
| 20 | 20 | |
| 21 | 21 | @Bean |
| 22 | - public BeanPostProcessor objectMapperBeanPostProcessor() { | |
| 23 | - return new BeanPostProcessor() { | |
| 24 | - | |
| 25 | - @Override | |
| 26 | - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | |
| 27 | - if (!(bean instanceof ObjectMapper)) { | |
| 28 | - return bean; | |
| 29 | - } | |
| 30 | - ObjectMapper objectMapper = (ObjectMapper) bean; | |
| 31 | - SimpleModule simpleModule = new SimpleModule(); | |
| 32 | - /* | |
| 33 | - * 1. 新增Long类型序列化规则,数值超过2^53-1,在JS会出现精度丢失问题,因此Long自动序列化为字符串类型 | |
| 34 | - * 2. 新增LocalDateTime序列化、反序列化规则 | |
| 35 | - */ | |
| 36 | - simpleModule | |
| 37 | - .addSerializer(Long.class, NumberSerializer.INSTANCE) | |
| 38 | - .addSerializer(Long.TYPE, NumberSerializer.INSTANCE) | |
| 39 | - .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) | |
| 40 | - .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); | |
| 41 | - | |
| 42 | - objectMapper.registerModules(simpleModule); | |
| 43 | - | |
| 44 | - JsonUtils.init(objectMapper); | |
| 45 | - log.info("初始化 jackson 自动配置"); | |
| 46 | - return bean; | |
| 47 | - } | |
| 48 | - }; | |
| 22 | + @SuppressWarnings("InstantiationOfUtilityClass") | |
| 23 | + public JsonUtils jsonUtils(List<ObjectMapper> objectMappers) { | |
| 24 | + // 1.1 创建 SimpleModule 对象 | |
| 25 | + SimpleModule simpleModule = new SimpleModule(); | |
| 26 | + simpleModule | |
| 27 | + // 新增 Long 类型序列化规则,数值超过 2^53-1,在 JS 会出现精度丢失问题,因此 Long 自动序列化为字符串类型 | |
| 28 | + .addSerializer(Long.class, NumberSerializer.INSTANCE) | |
| 29 | + .addSerializer(Long.TYPE, NumberSerializer.INSTANCE) | |
| 30 | + // 新增 LocalDateTime 序列化、反序列化规则 | |
| 31 | + .addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE) | |
| 32 | + .addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); | |
| 33 | + // 1.2 注册到 objectMapper | |
| 34 | + objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule)); | |
| 35 | + | |
| 36 | + // 2. 设置 objectMapper 到 JsonUtils { | |
| 37 | + JsonUtils.init(CollUtil.getFirst(objectMappers)); | |
| 38 | + log.info("[init][初始化 JsonUtils 成功]"); | |
| 39 | + return new JsonUtils(); | |
| 49 | 40 | } |
| 50 | 41 | |
| 51 | 42 | } | ... | ... |
yudao-server/src/main/resources/application-local.yaml
| ... | ... | @@ -156,9 +156,12 @@ logging: |
| 156 | 156 | cn.iocoder.yudao.module.bpm.dal.mysql: debug |
| 157 | 157 | cn.iocoder.yudao.module.infra.dal.mysql: debug |
| 158 | 158 | cn.iocoder.yudao.module.infra.dal.mysql.job.JobLogMapper: INFO # 配置 JobLogMapper 的日志级别为 info |
| 159 | + cn.iocoder.yudao.module.infra.dal.mysql.file.FileConfigMapper: INFO # 配置 FileConfigMapper 的日志级别为 info | |
| 159 | 160 | cn.iocoder.yudao.module.pay.dal.mysql: debug |
| 160 | 161 | cn.iocoder.yudao.module.pay.dal.mysql.notify.PayNotifyTaskMapper: INFO # 配置 JobLogMapper 的日志级别为 info |
| 161 | 162 | cn.iocoder.yudao.module.system.dal.mysql: debug |
| 163 | + cn.iocoder.yudao.module.system.dal.mysql.sensitiveword.SensitiveWordMapper: INFO # 配置 SensitiveWordMapper 的日志级别为 info | |
| 164 | + cn.iocoder.yudao.module.system.dal.mysql.sms.SmsChannelMapper: INFO # 配置 SmsChannelMapper 的日志级别为 info | |
| 162 | 165 | cn.iocoder.yudao.module.tool.dal.mysql: debug |
| 163 | 166 | cn.iocoder.yudao.module.member.dal.mysql: debug |
| 164 | 167 | cn.iocoder.yudao.module.trade.dal.mysql: debug | ... | ... |