我有一个枚举类型:
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),而不是名称。我可以通过
POST
json 将值发送给控制器,它可以正确地绑定到对象上。然而,当我从
GET
渲染JSON时,它仍然显示名称(员工、商店等),但我希望它仍然显示值。
@JsonValue
并回报结果。谢谢! - Gregg