如何在RequestParm中将多个值转换为枚举? [英] How to convert multiple values to enum in RequestParm?

查看:22
本文介绍了如何在RequestParm中将多个值转换为枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有这个枚举声明

I have this enum declaration below

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,如下所示

and I have a controller endpoint where the delete request holds the FamilyType as Requestparam like the following

public String deleteFamilyType(@PathVariable String userId, @Valid @RequestParam FamilyType familyType) {

从邮递员 familytype=firstname 发送时,它可以工作,但如果发送 familytype=nickname 然后我的转换器返回

while sending from postman familytype=firstname, it works but if sending familytype=nickname then my converter 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.

推荐答案

遵循本教程第 3 节,我们可以使用自定义转换器来覆盖默认转换 Enum#valueOf.

Following this tutorial section 3, we can use custom converter to override the default conversion 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配置中

And then add the converter to MVC configuration

@Configuration
public class AppConfig implements WebMvcConfigurer {
    // ...
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToFamilyTypeConverter());
    }
    // ...
}

这篇关于如何在RequestParm中将多个值转换为枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆