基于 枚举值 输出 枚举描述的 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":"字典"
}
上一篇:自定义RedisTemplate


下一篇:@JsonFormat与@DateTimeFormat注解的使用