• 基于 枚举值 输出 枚举描述的 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":"字典"
    }
    
  • 相关阅读:
    远程、标签
    NUnit单元测试资料汇总
    jdk1.6下载页面
    javac: cannot execute binary file
    how to remove MouseListener / ActionListener on a JTextField
    Linux下chkconfig命令详解(转)
    如何让vnc控制由默认的twm界面改为gnome?(转)
    winzip15.0注冊码
    微服务的优缺点
    站点建设10个最好的响应的HTML5滑块插件
  • 原文地址:https://www.cnblogs.com/akashicbrother/p/15439147.html
Copyright © 2020-2023  润新知