- 前言
在Java开发中我们为了避免过多的魔法值,使用枚举类来封装一些静态的状态代码。但是在将这些枚举的意思正确而全面的返回给前端却并不是那么顺利,我们通常会使用Jackson类库序列化对象为JSON,今天就来讲一个关于使用Jackson序列化枚举的通用性技巧。
2. 通用枚举范式
为了便于统一处理和规范统一的风格,建议指定一个统一的抽象接口,例如:
/**
* The interface Enumerator.
*/
public interface Enumerator {
/**
* Code integer.
*
* @return the integer
*/
Integer code();
/**
* Description string.
*
* @return the string
*/
String description();
}
我们来写一个实现来标识性别:
public enum GenderEnum implements Enumerator {
UNKNOWN(0, "未知"),
MALE(<