首页 > 解决方案 > Jackson 中小写 Java 枚举常量的更好解决方案

问题描述

我正在寻找一种更好(更简洁)的解决方案,以便在 Jackson 中将大写的 Java 枚举常量序列化/反序列化为小写,而不是这个样板代码:


    public enum GitLabPipelineStatusEnum {
        
        @JsonProperty("canceled")
        CANCELED,
        
        @JsonProperty("created")
        CREATED,
        
        @JsonProperty("failed")
        FAILED,
        
        @JsonProperty("manual")
        MANUAL,

标签: jackson

解决方案


public enum GitLabPipelineStatusEnum {
   
    CANCELED,
    CREATED,
    FAILED,
    MANUAL,

    @JsonValue
    public String toLowerCase() {
        return toString().toLowerCase();
    }
}

参考

https://www.baeldung.com/jackson-serialize-enums


推荐阅读