• 基于 枚举值 输出 枚举描述的 jackson 自定义注解方法


    @Documented
    @JacksonAnnotationsInside
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @JsonSerialize(using = EnumShowSerializer.class)
    public @interface JacksonEnumShow {
        /**
         * 要转换成的枚举
         *
         * @return
         */
        Class<? extends Enum<?>> using();
    
        /**
         * 是否复写本身
         *
         * @return
         */
        boolean self() default false;
    
        /**
         * 如果不复写本身新增字段是本身字段的增加的后缀
         * @return
         */
        String suffix() default "Text";
    }
    
    
    
    public class EnumShowSerializer extends JsonSerializer<Integer> implements ContextualSerializer {
    
        private JacksonEnumShow enumShow;
    
        public EnumShowSerializer() {
    
        }
    
        public EnumShowSerializer(final JacksonEnumShow enumShow) {
            this.enumShow = enumShow;
        }
    
    
        @SneakyThrows
        @Override
        public void serialize(Integer integer, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    
            if (null == integer || null == enumShow) {
                jsonGenerator.writeObject(integer);
                return;
            }
            String fieldName = jsonGenerator.getOutputContext().getCurrentName();
            Class<?>[] interfaces = enumShow.using().getInterfaces();
            String theMsg = null;
            if (interfaces.length > 0) {
    
                Class<?> baseInterface = interfaces[0];
                if (baseInterface.equals(BuzEnum.class)) {
                    Class newIns = enumShow.using();
                    Optional<? extends BuzEnum<?>> enumOpt = BuzEnum.of(newIns, integer);
                    if (enumOpt.isPresent()) {
                        theMsg = enumOpt.get().getValue();
                    }
                }
                if (baseInterface.equals(AuthBaseEnum.class)) {
                    Class newIns = enumShow.using();
                    Optional<? extends AuthBaseEnum<?>> enumOpt = AuthBaseEnum.of(newIns, integer);
                    if (enumOpt.isPresent()) {
                        theMsg = enumOpt.get().getValue();
                    }
                }
    
            } else {
                if (enumShow.using().isEnum()) {
                    Method codeMethod = enumShow.using().getDeclaredMethod("getCode");
                    Method descMethod = enumShow.using().getDeclaredMethod("getDesc");
                    for (Object obj : enumShow.using().getEnumConstants()) {
                        if (integer.equals(codeMethod.invoke(obj))) {
                            theMsg = descMethod.invoke(obj).toString();
                        }
                    }
                }
            }
            if (enumShow.self()) {
                jsonGenerator.writeString(theMsg);
            } else {
                jsonGenerator.writeNumber(integer);
                jsonGenerator.writeStringField(fieldName + enumShow.suffix(), theMsg);
            }
    
    
        }
    
        @Override
        public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
    
            JacksonEnumShow annotation = beanProperty.getAnnotation(JacksonEnumShow.class);
            if (annotation != null) {
                return new EnumShowSerializer(annotation);
            }
            return this;
        }
    }
    
    
    

    使用方式

    
        @Data
        @Builder
        public class TheClass {
            @JacksonEnumShow(using = FlagEnum.class, self = true)
            private Integer flag;
    
            @JacksonEnumShow(using = DictFromEnum.class)
            private Integer dict;
        }
    

    输出的json

    {
    	"flag":"是",
    	"dict":1,
    	"dictText":"字典"
    }
    
  • 相关阅读:
    TCP/IP笔记 一.综述
    Makefile的规则
    u盘安装ubuntu10.04 server.txt
    浅谈数据库技术,磁盘冗余阵列,IP分配,ECC内存,ADO,DAO,JDBC
    cocos2d-js 热更新具体解释(一)
    C#一个托付的样例
    JAVA学习之 异常处理机制
    阿里巴巴校招内推简历筛选方案
    《凑硬币》 动态规划算法入门
    android 读取xml
  • 原文地址:https://www.cnblogs.com/akashicbrother/p/15439147.html
Copyright © 2020-2023  润新知