SpringBoot实体类的枚举属性处理
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
// 假设有一个状态枚举类
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StatusEnum {
ACTIVE("active", "激活"),
INACTIVE("inactive", "未激活");
private String value;
private String description;
StatusEnum(String value, String description) {
this.value = value;
this.description = description;
}
@JsonValue
public String getValue() {
return value;
}
public String getDescription() {
return description;
}
}
// 实体类中使用状态枚举
public class User {
private String name;
private StatusEnum status;
// 省略其他属性和方法...
public StatusEnum getStatus() {
return status;
}
public void setStatus(StatusEnum status) {
this.status = status;
}
}
这个代码示例展示了如何在SpringBoot应用中定义一个枚举类,并在实体类中使用它。枚举类用@JsonFormat
和@JsonValue
注解来控制在JSON序列化和反序列化时枚举值的处理。这样,在与前端交互时,可以直接使用枚举值,而不是它们的字符串表示。
评论已关闭