休息控制器,提供枚举名称但将其值保存在数据库中
Rest controller, providing enum name but saving it's value in database
我的问题似乎既简单又复杂。我有一个 class 显示器,它有一个枚举,即 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;
}
}
}
现在,我有一个在 JSON 中接收显示的休息控制器,即
{"display": {"mode": "BIG"}}
它在 MongoDB 中保存为
{"display": {"mode": "BIG"}}
我想要的是,如果收到休息请求显示为
{"display": {"mode": "BIG"}} or
{"display": {"mode": "big"}} or anyCase insensitive value
它应该在数据库中保存为
{"display": {"mode": "display.mode.big"}}
当我想通过rest controller 读取Display out 时,它应该和数据库中保存的一样。
任何使用序列化器和反序列化器或其他任何解决方案。谢谢
使用@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);
}
我的问题似乎既简单又复杂。我有一个 class 显示器,它有一个枚举,即 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;
}
}
}
现在,我有一个在 JSON 中接收显示的休息控制器,即
{"display": {"mode": "BIG"}}
它在 MongoDB 中保存为
{"display": {"mode": "BIG"}}
我想要的是,如果收到休息请求显示为
{"display": {"mode": "BIG"}} or
{"display": {"mode": "big"}} or anyCase insensitive value
它应该在数据库中保存为
{"display": {"mode": "display.mode.big"}}
当我想通过rest controller 读取Display out 时,它应该和数据库中保存的一样。
任何使用序列化器和反序列化器或其他任何解决方案。谢谢
使用@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);
}