mirror of https://gitee.com/stylefeng/roses
【7.2.5】【wrapper】更新一些类名
parent
b911d7b899
commit
4ac9637014
|
@ -24,13 +24,13 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.rule.annotation;
|
||||
|
||||
import cn.stylefeng.roses.kernel.rule.base.JsonFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* json字段的格式化,可以将多种情景的id,转化为具体的具有可读性的名称
|
||||
* json字段的格式化,可以将类似id值,转化为具体的具有可读性的名称,例如:用户id -> 用户姓名
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/6 11:34
|
||||
|
@ -38,7 +38,7 @@ import java.lang.annotation.*;
|
|||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
public @interface JsonFieldFormat {
|
||||
public @interface SimpleFieldFormat {
|
||||
|
||||
/**
|
||||
* 字段格式化的类型 详情见:{@link FormatTypeEnum}
|
||||
|
@ -50,6 +50,6 @@ public @interface JsonFieldFormat {
|
|||
/**
|
||||
* 具体处理值转化的过程【必传】
|
||||
*/
|
||||
Class<? extends JsonFieldFormatProcess> processClass();
|
||||
Class<? extends SimpleFieldFormatProcess> processClass();
|
||||
|
||||
}
|
|
@ -6,7 +6,7 @@ package cn.stylefeng.roses.kernel.rule.base;
|
|||
* @author fengshuonan
|
||||
* @date 2022/9/6 11:54
|
||||
*/
|
||||
public interface JsonFieldFormatProcess {
|
||||
public interface SimpleFieldFormatProcess {
|
||||
|
||||
/**
|
||||
* 是否可以进行格式化转化
|
|
@ -1,7 +0,0 @@
|
|||
/**
|
||||
* 系统默认的一些转化实现,例如对枚举的转化,转化逻辑较为固定
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/6 16:30
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.wrapper.field.impls;
|
|
@ -1,13 +1,14 @@
|
|||
package cn.stylefeng.roses.kernel.wrapper.field.jackson;
|
||||
|
||||
import cn.stylefeng.roses.kernel.rule.annotation.JsonFieldFormat;
|
||||
import cn.stylefeng.roses.kernel.rule.base.JsonFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.annotation.SimpleFieldFormat;
|
||||
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
|
||||
import cn.stylefeng.roses.kernel.wrapper.field.simple.SimpleFieldFormatSerializer;
|
||||
import com.fasterxml.jackson.databind.introspect.Annotated;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
|
||||
/**
|
||||
* Json序列化,注解拦截器,针对自定义注解@JsonFieldFormat进行拓展性序列化
|
||||
* Json序列化,注解拦截器,针对自定义注解进行拓展性序列化
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/6 13:56
|
||||
|
@ -21,7 +22,7 @@ public class CustomJacksonIntrospector extends JacksonAnnotationIntrospector {
|
|||
|
||||
@Override
|
||||
public Object findSerializer(Annotated annotated) {
|
||||
JsonFieldFormat formatter = annotated.getAnnotation(JsonFieldFormat.class);
|
||||
SimpleFieldFormat formatter = annotated.getAnnotation(SimpleFieldFormat.class);
|
||||
|
||||
if (formatter == null || formatter.processClass() == null) {
|
||||
return super.findSerializer(annotated);
|
||||
|
@ -31,10 +32,10 @@ public class CustomJacksonIntrospector extends JacksonAnnotationIntrospector {
|
|||
FormatTypeEnum formatTypeEnum = formatter.formatType();
|
||||
|
||||
// 获取具体的处理方法
|
||||
Class<? extends JsonFieldFormatProcess> process = formatter.processClass();
|
||||
Class<? extends SimpleFieldFormatProcess> process = formatter.processClass();
|
||||
|
||||
// 创建对应的序列化模式
|
||||
return new CustomJsonSerializer(formatTypeEnum, process);
|
||||
return new SimpleFieldFormatSerializer(formatTypeEnum, process);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
* 针对jackson框架的一些拓展
|
||||
* <p>
|
||||
* 增加某些机制,让jackson序列化json的时候,能识别我们自己的注解 @JsonFieldFormat {@link cn.stylefeng.roses.kernel.rule.annotation.JsonFieldFormat}
|
||||
* 增加某些机制,让jackson序列化json的时候,能识别我们自己的注解 @SimpleFieldFormat {@link cn.stylefeng.roses.kernel.rule.annotation.SimpleFieldFormat}
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.wrapper.field.jackson;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package cn.stylefeng.roses.kernel.wrapper.field.jackson;
|
||||
package cn.stylefeng.roses.kernel.wrapper.field.simple;
|
||||
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.stylefeng.roses.kernel.rule.base.JsonFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.base.SimpleFieldFormatProcess;
|
||||
import cn.stylefeng.roses.kernel.rule.enums.FormatTypeEnum;
|
||||
import cn.stylefeng.roses.kernel.wrapper.api.constants.WrapperConstants;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
@ -13,13 +13,13 @@ import java.io.IOException;
|
|||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* 针对@JsonFieldFormat注解的具体序列化过程
|
||||
* 针对@SimpleFieldFormat注解的具体序列化过程
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2022/9/6 14:09
|
||||
*/
|
||||
@Slf4j
|
||||
public class CustomJsonSerializer extends JsonSerializer<Object> {
|
||||
public class SimpleFieldFormatSerializer extends JsonSerializer<Object> {
|
||||
|
||||
/**
|
||||
* 序列化类型,覆盖还是wrapper模式
|
||||
|
@ -29,9 +29,9 @@ public class CustomJsonSerializer extends JsonSerializer<Object> {
|
|||
/**
|
||||
* 具体序列化过程
|
||||
*/
|
||||
private final Class<? extends JsonFieldFormatProcess> processClass;
|
||||
private final Class<? extends SimpleFieldFormatProcess> processClass;
|
||||
|
||||
public CustomJsonSerializer(FormatTypeEnum formatTypeEnum, Class<? extends JsonFieldFormatProcess> processClass) {
|
||||
public SimpleFieldFormatSerializer(FormatTypeEnum formatTypeEnum, Class<? extends SimpleFieldFormatProcess> processClass) {
|
||||
this.formatTypeEnum = formatTypeEnum;
|
||||
this.processClass = processClass;
|
||||
}
|
||||
|
@ -43,22 +43,22 @@ public class CustomJsonSerializer extends JsonSerializer<Object> {
|
|||
String fieldName = jsonGenerator.getOutputContext().getCurrentName();
|
||||
|
||||
// 创建具体字段转化的实现类
|
||||
JsonFieldFormatProcess jsonFieldFormatProcess = null;
|
||||
SimpleFieldFormatProcess simpleFieldFormatProcess = null;
|
||||
try {
|
||||
jsonFieldFormatProcess = processClass.newInstance();
|
||||
simpleFieldFormatProcess = processClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
log.error("执行json的字段序列化出错", e);
|
||||
return;
|
||||
}
|
||||
|
||||
// 判断当前字段值是否可以转化
|
||||
boolean canFormat = jsonFieldFormatProcess.canFormat(originValue);
|
||||
boolean canFormat = simpleFieldFormatProcess.canFormat(originValue);
|
||||
if (!canFormat) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行转化,获取转化过的值
|
||||
Object formattedValue = jsonFieldFormatProcess.formatProcess(originValue);
|
||||
Object formattedValue = simpleFieldFormatProcess.formatProcess(originValue);
|
||||
|
||||
try {
|
||||
// 如果转化模式是替换类型
|
Loading…
Reference in New Issue