如何在 RequestParm 中将多个值转换为枚举?
How to convert multiple values to enum in RequestParm?
我在下面有这个枚举声明
public enum FamilyType {
FIRSTNAME("firstname"),
LASTNAME("lastname");
private final String type;
FamilyType(String type) {
this.type = type;
}
public static FamilyType fromString(String type) {
for (FamilyType t : FamilyType.values()) {
if (t.type.equals(type)) {
return t;
}
}
return converter(type);
}
@Deprecated
private static FamilyType converter(String value) {
if ("NICKNAME".equalsIgnoreCase(value)) {
return FIRSTNAME;
}
if ("SURENAME".equalsIgnoreCase(value)) {
return LASTNAME;
}
throw new InvalidFileNameException("my-enum", value);
}
}
我有一个控制器端点,删除请求将 FamilyType
保存为 Requestparam
,如下所示
public String deleteFamilyType(@PathVariable String userId, @Valid @RequestParam FamilyType familyType) {
从邮递员 familytype=firstname
发送时,它有效,但如果发送 familytype=nickname
,那么我的转换器 returns
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type 'FamilyType';
nested exception is java.lang.IllegalArgumentException:
No enum constant FamilyType.NICKNAME
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:133)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.
在this tutorial section 3之后,我们可以使用自定义转换器来覆盖默认转换Enum#valueOf
。
public class StringToFamilyTypeConverter implements Converter<String, FamilyType> {
@Override
public FamilyType convert(String source) {
if ("NICKNAME".equalsIgnoreCase(source)) {
return FamilyType.FIRSTNAME;
}
if ("SURENAME".equalsIgnoreCase(source)) {
return FamilyType.LASTNAME;
}
return FamilyType.valueOf(source.toUpperCase());
}
}
然后将转换器添加到 MVC 配置
@Configuration
public class AppConfig implements WebMvcConfigurer {
// ...
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToFamilyTypeConverter());
}
// ...
}
我在下面有这个枚举声明
public enum FamilyType {
FIRSTNAME("firstname"),
LASTNAME("lastname");
private final String type;
FamilyType(String type) {
this.type = type;
}
public static FamilyType fromString(String type) {
for (FamilyType t : FamilyType.values()) {
if (t.type.equals(type)) {
return t;
}
}
return converter(type);
}
@Deprecated
private static FamilyType converter(String value) {
if ("NICKNAME".equalsIgnoreCase(value)) {
return FIRSTNAME;
}
if ("SURENAME".equalsIgnoreCase(value)) {
return LASTNAME;
}
throw new InvalidFileNameException("my-enum", value);
}
}
我有一个控制器端点,删除请求将 FamilyType
保存为 Requestparam
,如下所示
public String deleteFamilyType(@PathVariable String userId, @Valid @RequestParam FamilyType familyType) {
从邮递员 familytype=firstname
发送时,它有效,但如果发送 familytype=nickname
,那么我的转换器 returns
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type 'FamilyType';
nested exception is java.lang.IllegalArgumentException:
No enum constant FamilyType.NICKNAME
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:133)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.
在this tutorial section 3之后,我们可以使用自定义转换器来覆盖默认转换Enum#valueOf
。
public class StringToFamilyTypeConverter implements Converter<String, FamilyType> {
@Override
public FamilyType convert(String source) {
if ("NICKNAME".equalsIgnoreCase(source)) {
return FamilyType.FIRSTNAME;
}
if ("SURENAME".equalsIgnoreCase(source)) {
return FamilyType.LASTNAME;
}
return FamilyType.valueOf(source.toUpperCase());
}
}
然后将转换器添加到 MVC 配置
@Configuration
public class AppConfig implements WebMvcConfigurer {
// ...
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new StringToFamilyTypeConverter());
}
// ...
}