我的问题似乎既简单又复杂。我有一个 Display 类,它有一个枚举,即 DisplayMode。
public class Display {
private DisplayMode mode;
//getters and setters
public enum DisplayMode {
BIG("display.mode.big"),
SMALL("display.mode.small"),
MEDIUM("display.mode.medium");
private String modeValue;
DisplayMode(String modeValue) {
this.modeValue = modeValue;
}
public String toString() {
return this.name() + "/" + this.modeValue;
}
public String getModeValue() {
return this.modeValue;
}
}
}
现在,我有一个休息 Controller ,它接收 JSON 格式的显示,即
{"display": {"mode": "BIG"}}
它在 MongoDB 中保存为
{"display": {"mode": "BIG"}}
我想要的是,如果收到其余请求显示为
{"display": {"mode": "BIG"}} or
{"display": {"mode": "big"}} or anyCase insensitive value
它应该在数据库中保存为
{"display": {"mode": "display.mode.big"}}
当我想通过休息 Controller 读取显示时,它应该与数据库中保存的相同。
使用序列化器和反序列化器或其他任何东西的任何解决方案。谢谢
最佳答案
使用@JsonValue
保存值&&@JsonCreator
进行反序列化。
@JsonValue
final String modeValue() {
return this.modeValue;
}
对于解串器:
@JsonCreator
public static DisplayMode forValue(String v) {
return Arrays.stream(DisplayMode.values())
.filter(dm -> dm.name().equalsIgnoreCase(v))
.findAny().orElse(null);
}
关于java - 休息 Controller ,提供枚举名称但将其值保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51675889/