以下为我经常使用的做法,定义一个 enum,enum 里定义好 Json 、Jpa 等转换规则。
仅供参考:
```
@
Getter@
AllArgsConstructorpublic enum State {
UNKNOWN("未知"),
ACTIVE("正常"),
INACTIVE("封禁"),
;
private final String desc;
@
JsonCreator public static State fromStr(String strValue) {
return
Arrays.stream(State.values()).filter(value -> value.value().equals(strValue)).findFirst().orElse(UNKNOWN);
}
@
JsonValue public String value() {
return
this.name();
}
@
Converter(autoApply = true)
public static class DatabaseColumnConverter implements AttributeConverter<State, String> {
public String convertToDatabaseColumn(State state) {
return state != null ?
state.name() :
UNKNOWN.name();
}
public State convertToEntityAttribute(String name) {
return fromStr(name);
}
}
}
```