我有以下枚举:
public enum NotificationType {
Store("S"),
Employee("E"),
Department("D"),
All("A");
public String value;
NotificationType(String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
@JsonCreator
public static NotificationType fromValue(String value) {
for (NotificationType type : NotificationType.values()) {
if (type.value.equals(value)) {
return type;
}
}
throw new IllegalArgumentException();
}
}
我创建了一个转换器,以便在将枚举保存到数据库时,它会保留值(S、E、D 或 A)而不是名称。我可以使用值将 json POST
json 正确绑定(bind)到对象。
但是,当我从 GET
呈现 JSON 时,它仍然显示名称(Employee、Store 等),我希望它仍然显示值。
最佳答案
因为您的toString
方法返回您想要用来表示您的枚举的值,您可以用@JsonValue
注释它来告诉Jackson 返回值表示该值枚举的。
关于java - 想要获取枚举值而不是 JSON 中的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38268485/