As far as I know, there is no way to do this magically with an annotation or so. What you could do is implement a custom converter and convert the value inputted into your enum
like this:
public class MyCustomEnumConverter implements Converter<String, YourEnum>
{
@Override
public YourEnum convert(String source) {
try
{
if(isNumeric(source)) // If the string passed is a Number
{
int index = Integer.parseInt(source);
return YourEnum.values()[index]; // Get the Enum at the specific index
}
// Else if it is a string..
return YourEnum.valueOf(source);
} catch(Exception e) {
return null; // or whatever you need
}
}
private static boolean isNumeric(String str) {
try {
Integer.parseInt(str);
return true;
} catch(NumberFormatException e){
return false;
}
}
}
And then register your Converter
:
@Configuration
public class MyConfig extends WebMvcConfigurationSupport {
@Override
public FormattingConversionService mvcConversionService() {
FormattingConversionService f = super.mvcConversionService();
f.addConverter(new MyCustomEnumConverter());
return f;
}
}
You could improve the logic of MyCustomEnumConverter
but for the sake of simplicity, I left it at that.
If you want to apply the specific Converter
to just one controller check this answer.