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);
}
}
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) {
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.