[英]Wanting to get Enum Value instead of name in JSON
I've got the following enum: 我有以下枚举:
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();
}
}
I've created a converter so that when the enum is saved to the database, it persists the value (S, E, D or A) instead of the name. 我创建了一个转换器,以便将枚举保存到数据库时,它会保留值(S,E,D或A)而不是名称。 And I can
POST
json to the controller with the value and it binds to the object correctly. 我还可以
POST
JSON与值控制器和它正确地绑定到该对象。
However, when I render the JSON from a GET
it is still displaying the name (Employee, Store, etc) and I would prefer that it still show the value. 但是,当我从
GET
渲染JSON时,它仍显示名称(Employee,Store等),我希望它仍显示值。
因为您的toString
方法返回了您要用来表示枚举的值,所以您可以使用@JsonValue
进行注释,以告诉Jackson返回值表示枚举的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.