Skip to content

Commit

Permalink
添加 优雅的使用枚举参数
Browse files Browse the repository at this point in the history
  • Loading branch information
andanyoung committed Nov 1, 2023
1 parent d43abed commit 2810434
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
91 changes: 91 additions & 0 deletions enum-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 优雅的使用枚举参数

## 接受格式为 json (Content-Type: application/json)

推荐使用 jackson 注解,最终将显示 value 字段,前端传值也是 传 int型的 value 字段

```
@Getter
@AllArgsConstructor
public enum UserStatus {
NORMAL(1, "正常"),
FREEZE(2, "冻结");
@JsonValue
private final int value;
private final String text;
@JsonCreator
public static UserStatus valueOf(int value) {
for (UserStatus status : UserStatus.values()) {
if (status.value == value) {
return status;
}
}
return null;
}
}
```

## 接受格式为 form (GET 或者 POST FORM)

列如

```
@RestController
@RequestMapping
public class EnumConverterController {
@RequestMapping("UserStatus")
public (@UserStatus userStatusRequestParam UserStatus userStatus) {
return userStatus;
}
}
```

此时使用不到jackson注解`@JsonValue`,因为他使用了 Spring 的 Converter,所以我们需要自定义Converter
参考 `EnumConverterFactory`

```
@RequiredArgsConstructor
public class EnumConverterFactory implements ConverterFactory<String, Enum> {
@SuppressWarnings("rawtypes")
private static final Map<Class, Converter> CONVERTERS = new ConcurrentHashMap<>(64);
private final List<EnumConverter> enumConverters;
@Override
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return CONVERTERS.computeIfAbsent(targetType, (key) -> new StringToEnum<T>(targetType, enumConverters));
}
private static class StringToEnum<T extends Enum> implements Converter<String, T> {
private final Class<T> enumType;
private final List<EnumConverter> enumConverters;
StringToEnum(Class<T> enumType, List<EnumConverter> enumConverters) {
this.enumType = enumType;
this.enumConverters = enumConverters;
}
@Override
public T convert(String source) {
if (source.isEmpty()) {
// It's an empty enum identifier: reset the enum value to null.
return null;
}
for (EnumConverter enumConverter : enumConverters) {
T convert = enumConverter.convert(source, enumType);
if (convert != null) {
return convert;
}
}
return null;
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ public UserStatus userStatus(@RequestParam UserStatus userStatus) {

return userStatus;
}

}

0 comments on commit 2810434

Please sign in to comment.