枚举类型在定义应用程序域内有限且明确的值集方面非常有效,有助于避免代码中出现无效状态。
应用场景
以下以一个Spring Boot 3.3.x 和 MongoDB 实现的笔记Web应用为例,演示枚举值的序列化和反序列化方法。 我们将定义一个Type枚举,表示待办事项的类型:事件和活动。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public enum Type { EVENT("event"), ACTIVITY("activity");
private final String value;
Type(String value) { this.value = value; }
public String getValue() { return value; }
private static final Map<String, Type> ENUM_MAP;
static { ENUM_MAP = Arrays.stream(values()) .collect(Collectors.toMap(Type::getValue, t -> t)); }
public static Type fromString(String value) { return ENUM_MAP.get(value); } } |
我们的Todo实体类:
1 2 3 4 5 6 7 |
public class Todo { private String id; private String name; private boolean completed; private Type type; // ... getters and setters ... } |
我们将探讨以下场景:
1. 枚举作为查询参数
此场景只需反序列化,将字符串值转换为枚举。 以下是一个控制器方法片段,用于根据类型读取待办事项,类型作为查询参数传递:
1 2 3 4 |
@GetMapping("/todos") public Collection<Todo> read(@RequestParam(required = false) Type type) { // ... implementation ... } |
由于查询参数为字符串,我们需要一个转换器:
1 2 3 4 5 6 |
public class StringToTypeConverter implements Converter<String, Type> { @Override public Type convert(String source) { return Type.fromString(source); } } |
在配置类中注册转换器:
1 2 3 4 5 6 7 |
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverter(new StringToTypeConverter()); } } |
现在,当Type用作@RequestParam时,StringToTypeConverter将尝试将字符串值转换为枚举。
2. 枚举作为JSON请求体的一部分
为了正确处理JSON请求体中的枚举字段,我们需要在Type枚举中添加@JsonValue注解:
(上面的Type枚举已包含必要的fromString和ENUM_MAP)
3. 枚举作为MongoDB文档字段
为了在MongoDB中管理枚举的序列化/反序列化,我们需要使用@ValueConverter注解,并将文档字段与一个自定义的PropertyValueConverter类关联:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Document(collection = "todos") public class Todo { @Id private String id; // ... other fields ... @ValueConverter(MongoEnumConverter.class) private Type type; // ... getters and setters ... }
public class MongoEnumConverter implements PropertyValueConverter<Type, String> { @Override public Type convert(String source) { return Type.fromString(source); }
@Override public String write(Type value) { return value.getValue(); } } |
MongoEnumConverter提供读写方法来管理转换。
总结
本文介绍了在常见Web场景中处理枚举序列化/反序列化的几种方法。 Spring和Jackson库提供了简化此过程的工具。 完整的代码示例可在一个公开的GitLab仓库中找到。(此处省略GitLab仓库链接,因为我没有访问权限)
本文中提供的代码已获得CC0许可。