mirror of https://gitee.com/stylefeng/roses
【7.6.0】【rule】【format】增加一个根据字典类型编码+字典编码进行转义中文的注解
parent
ebccde979e
commit
3e0f5960a2
|
@ -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字段的格式化,可以将字典编码转化为具体的中文含义
|
||||
* <p>
|
||||
* 因为不同字典类型,编码可能有重复的,所以需要指定一个字典类型编码,确定是在哪个字典类型下的转义
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @since 2023/6/27 21:01
|
||||
*/
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
public @interface DictCodeFieldFormat {
|
||||
|
||||
/**
|
||||
* 字段格式化的类型 详情见:{@link FormatTypeEnum}
|
||||
* <p>
|
||||
* 默认采用包装型,不改变原有的字段
|
||||
*/
|
||||
FormatTypeEnum formatType() default FormatTypeEnum.ADD_FIELD;
|
||||
|
||||
/**
|
||||
* 字典类型的编码【必传】
|
||||
*/
|
||||
String dictTypeCode();
|
||||
|
||||
}
|
|
@ -24,6 +24,14 @@
|
|||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--字典模块的api-->
|
||||
<!--新增加了一个字典编码的转化-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>dict-api</artifactId>
|
||||
<version>${roses.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--jackson相关基础依赖-->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
|
|
|
@ -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<Object> {
|
||||
|
||||
/**
|
||||
* 序列化类型,覆盖还是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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue