From 3e0f5960a24c11740642a6e4e25f9113cdcd2fa8 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Tue, 27 Jun 2023 21:13:50 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=907.6.0=E3=80=91=E3=80=90rule=E3=80=91?= =?UTF-8?q?=E3=80=90format=E3=80=91=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=AD=97=E5=85=B8=E7=B1=BB=E5=9E=8B=E7=BC=96?= =?UTF-8?q?=E7=A0=81+=E5=AD=97=E5=85=B8=E7=BC=96=E7=A0=81=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E8=BD=AC=E4=B9=89=E4=B8=AD=E6=96=87=E7=9A=84=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rule/annotation/DictCodeFieldFormat.java | 56 +++++++++++++ kernel-d-wrapper/wrapper-field-sdk/pom.xml | 8 ++ .../field/dict/DictCodeFormatSerializer.java | 79 +++++++++++++++++++ .../jackson/CustomJacksonIntrospector.java | 15 ++++ 4 files changed, 158 insertions(+) create mode 100644 kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/DictCodeFieldFormat.java create mode 100644 kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/dict/DictCodeFormatSerializer.java diff --git a/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/DictCodeFieldFormat.java b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/DictCodeFieldFormat.java new file mode 100644 index 000000000..9dad33f2c --- /dev/null +++ b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/DictCodeFieldFormat.java @@ -0,0 +1,56 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.rule.annotation; + +import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum; + +import java.lang.annotation.*; + +/** + * json字段的格式化,可以将字典编码转化为具体的中文含义 + *

+ * 因为不同字典类型,编码可能有重复的,所以需要指定一个字典类型编码,确定是在哪个字典类型下的转义 + * + * @author fengshuonan + * @since 2023/6/27 21:01 + */ +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD}) +public @interface DictCodeFieldFormat { + + /** + * 字段格式化的类型 详情见:{@link FormatTypeEnum} + *

+ * 默认采用包装型,不改变原有的字段 + */ + FormatTypeEnum formatType() default FormatTypeEnum.ADD_FIELD; + + /** + * 字典类型的编码【必传】 + */ + String dictTypeCode(); + +} diff --git a/kernel-d-wrapper/wrapper-field-sdk/pom.xml b/kernel-d-wrapper/wrapper-field-sdk/pom.xml index 8bdc769ad..d47c4550d 100644 --- a/kernel-d-wrapper/wrapper-field-sdk/pom.xml +++ b/kernel-d-wrapper/wrapper-field-sdk/pom.xml @@ -24,6 +24,14 @@ ${roses.version} + + + + cn.stylefeng.roses + dict-api + ${roses.version} + + com.fasterxml.jackson.core diff --git a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/dict/DictCodeFormatSerializer.java b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/dict/DictCodeFormatSerializer.java new file mode 100644 index 000000000..f86908fbc --- /dev/null +++ b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/dict/DictCodeFormatSerializer.java @@ -0,0 +1,79 @@ +package cn.stylefeng.roses.kernel.wrapper.field.dict; + +import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.dict.api.context.DictContext; +import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum; +import cn.stylefeng.roses.kernel.rule.util.MixFieldTypeUtil; +import cn.stylefeng.roses.kernel.wrapper.field.util.CommonFormatUtil; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; + +/** + * 针对@DictCodeFieldFormat注解的具体序列化过程 + * + * @author fengshuonan + * @since 2023/6/27 21:05 + */ +@Slf4j +public class DictCodeFormatSerializer extends JsonSerializer { + + /** + * 序列化类型,覆盖还是wrapper模式 + */ + private final FormatTypeEnum formatTypeEnum; + + /** + * 字典类型编码 + */ + private final String dictTypeCode; + + public DictCodeFormatSerializer(FormatTypeEnum formatTypeEnum, String dictTypeCode) { + this.formatTypeEnum = formatTypeEnum; + this.dictTypeCode = dictTypeCode; + } + + @Override + public void serialize(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { + try { + this.action(originValue, jsonGenerator, serializerProvider); + } catch (Exception e) { + log.error("执行json的字段序列化出错", e); + // 报错后继续写入原始值,否则会响应的json不是规范的json + jsonGenerator.writeObject(originValue); + } + } + + /** + * 真正处理序列化的逻辑 + * + * @author fengshuonan + * @since 2022/9/7 11:11 + */ + private void action(Object originValue, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws + InstantiationException, IllegalAccessException, IOException { + + // 参数值为空,直接跳过格式化 + if (ObjectUtil.isEmpty(originValue)) { + jsonGenerator.writeObject(originValue); + return; + } + + // 判断当前字段类型是否是String类型,如果不是String类型则直接跳过,因为字典编码是String类型的 + boolean stringFlag = MixFieldTypeUtil.whetherAssignClass(originValue, String.class); + if (!stringFlag) { + jsonGenerator.writeObject(originValue); + return; + } + + // 执行转化,根据字典类型编码,以及字典的编码,获取指定字典的中文名称 + String dictName = DictContext.me().getDictName(this.dictTypeCode, originValue.toString()); + + // 将转化的值,根据策略,进行写入到渲染的json中 + CommonFormatUtil.writeField(formatTypeEnum, originValue, dictName, jsonGenerator); + } + +} diff --git a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/jackson/CustomJacksonIntrospector.java b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/jackson/CustomJacksonIntrospector.java index e61b16c8a..36b97d74c 100644 --- a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/jackson/CustomJacksonIntrospector.java +++ b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/jackson/CustomJacksonIntrospector.java @@ -1,10 +1,12 @@ package cn.stylefeng.roses.kernel.wrapper.field.jackson; +import cn.stylefeng.roses.kernel.rule.annotation.DictCodeFieldFormat; import cn.stylefeng.roses.kernel.rule.annotation.EnumFieldFormat; import cn.stylefeng.roses.kernel.rule.annotation.SimpleFieldFormat; import cn.stylefeng.roses.kernel.rule.base.ReadableEnum; import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess; import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum; +import cn.stylefeng.roses.kernel.wrapper.field.dict.DictCodeFormatSerializer; import cn.stylefeng.roses.kernel.wrapper.field.enums.EnumFieldFormatDeserializer; import cn.stylefeng.roses.kernel.wrapper.field.enums.EnumFieldFormatSerializer; import cn.stylefeng.roses.kernel.wrapper.field.simple.SimpleFieldFormatSerializer; @@ -53,6 +55,19 @@ public class CustomJacksonIntrospector extends JacksonAnnotationIntrospector { return new EnumFieldFormatSerializer(formatTypeEnum, process); } + // 字典编码的格式化转化过程 + DictCodeFieldFormat dictCodeFieldFormat = annotated.getAnnotation(DictCodeFieldFormat.class); + if (dictCodeFieldFormat != null && dictCodeFieldFormat.dictTypeCode() != null) { + // 获取格式化处理的方式 + FormatTypeEnum formatTypeEnum = dictCodeFieldFormat.formatType(); + + // 获取指定的字典类型的编码 + String dictTypeCode = dictCodeFieldFormat.dictTypeCode(); + + // 创建对应的序列化模式 + return new DictCodeFormatSerializer(formatTypeEnum, dictTypeCode); + } + return super.findSerializer(annotated); }