Within our controllers we want use enums as method parameters:
@GetMapping("/feed")
public void feed(@RequestParam myEnum: MyEnum)
This works until we try to use strings which doesn't match by case. For that I found a similar question:
This however requires setting handler for each enum. I would rather like to implement my custom handler for all enums. Something like:
val ordinal = param.toString().toIntOrNull()
// Reflection on built-in Kotlin types is not yet fully supported... so ...
return if (null == ordinal)
try {
type.jvmErasure.java.methods.find { it.name == "valueOf" }!!.invoke(null, param.toString())
} catch (e: Exception) {
type.jvmErasure.java.methods.find { it.name == "valueOf" }!!.invoke(null, param.toString().toUpperCase())
}
else
type.jvmErasure.java.enumConstants[ordinal]
Sorry this is written in Kotlin, but shouldn't be hard to understand. It basically tries valueOf on the original string, then on uppercase version and if it is number, it uses ordinal value of the enum.
I am looking for a general solution, not handler for single enum.