Java 如何在RequestParm中将多个值转换为枚举?

Java 如何在RequestParm中将多个值转换为枚举?,java,spring-boot,enums,http-request-parameters,Java,Spring Boot,Enums,Http Request Parameters,我在下面有这个枚举声明 公共枚举家族类型{ 名字(“名字”), 姓氏(“姓氏”); 私有最终字符串类型; 家庭类型(字符串类型){ this.type=type; } 公共静态FamilyType fromString(字符串类型){ 对于(FamilyType t:FamilyType.values()){ 如果(t.type.等于(type)){ 返回t; } } 返回转换器(类型); } @不赞成 专用静态家庭类型转换器(字符串值){ if(“昵称”.equalsIgnoreCase(值)

我在下面有这个枚举声明

公共枚举家族类型{
名字(“名字”),
姓氏(“姓氏”);
私有最终字符串类型;
家庭类型(字符串类型){
this.type=type;
}
公共静态FamilyType fromString(字符串类型){
对于(FamilyType t:FamilyType.values()){
如果(t.type.等于(type)){
返回t;
}
}
返回转换器(类型);
}
@不赞成
专用静态家庭类型转换器(字符串值){
if(“昵称”.equalsIgnoreCase(值)){
返回名字;
}
如果(“SURENAME.equalsIgnoreCase(值)){
返回姓氏;
}
抛出新的InvalidFileNameException(“我的枚举”,值);
}
}
我有一个控制器端点,其中delete请求将
FamilyType
保存为
Requestparam
,如下所示

public String deleteFamilyType(@PathVariable String userId,@Valid@RequestParam FamilyType FamilyType){
当从邮递员发送时,它可以工作,但如果发送时,我的转换器返回

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.
接下来,我们可以使用自定义转换器覆盖默认转换
Enum#valueOf

公共类StringToFamilyTypeConverter实现转换器{
@凌驾
公共FamilyType转换(字符串源){
if(“昵称”.equalsIgnoreCase(来源)){
返回FamilyType.FIRSTNAME;
}
如果(“SURENAME.equalsIgnoreCase(来源)){
返回FamilyType.LASTNAME;
}
返回FamilyType.valueOf(source.toUpperCase());
}
}
然后将转换器添加到MVC配置中

@配置
公共类AppConfig实现WebMVCConfiguer{
// ...
@凌驾
公共void addFormatters(FormatterRegistry注册表){
addConverter(新的StringToFamilyTypeConverter());
}
// ...
}

您是否遗漏了将昵称添加为枚举值?我不必这样做,因为有转换器来检查它。想法是映射iti仍然会遇到相同的错误:/
MethodArgumentTypeMismatchException:未能将“java.lang.String”类型的值转换为所需类型
您可以编辑帖子以包含完整的堆栈跟踪吗?更新了帖子对于@requestBody,因此,如果枚举值是body的一部分,但对于requestparam weired,则它甚至不会在StringToFamilyTypeConverter类中的convert方法中发出COMM