Get enum constant from @JsonProperty value

前端 未结 1 762
自闭症患者
自闭症患者 2021-01-26 13:24

I have an Enum marked with @JsonProperty for JSON serialization/deserialization with Jackson and would like to get the enum value for a given String JsonProperty:



        
相关标签:
1条回答
  • 2021-01-26 14:09

    The desired result can be achieved through the following method:

    public static <T extends Enum<T>> T getEnumValueFromJsonProperty(Class<T> enumClass, String jsonPropertyValue) {
        Field[] fields = enumClass.getFields();
        for (int i=0; i<fields.length; i++) {
            if (fields[i].getAnnotation(JsonProperty.class).value().equals(jsonPropertyValue)) {
                return Enum.valueOf(enumClass, fields[i].getName());
            }
        }
        return null;
    }
    
    0 讨论(0)
提交回复
热议问题