Very often, enum values should be in small letters, but Kotlin does not expect them. Therefore, this behavior needs to be further adjusted.
If it is necessary to use an enumeration in the form of a request or response, then this is so:
import com.fasterxml.jackson.annotation.JsonValue
enum class SampleEnum(@field:JsonValue val value: String) {
VALUE_1("value_1"),
OTHER("other"),
}
(it is the “field” that can be “retrieved” as well as “used”; hope that in some case this approach will not work.)
There is no need to re-read the query and use it in the query (query parameters), as this does not work. Why? JSON (and related libraries) are used for the request and response body, and Spring, a dynamic conversion mechanism, is used for the request parameters.
Nevertheless, it is possible to find a universal converter for enumeration (idea from public, implementation in Kotlin is mine):
@Component
class JacksonEnumConverter(private val mapper: ObjectMapper): GenericConverter {
private val types = setOf(
ConvertiblePair(String::class.java, Enum::class.java),
)
override fun getConvertibleTypes() = types
override fun convert(
source: Any?,
sourceType: TypeDescriptor,
targetType: TypeDescriptor,
): Any? {
if (source == null) return null
try {
return mapper.readValue("\"$source\"", targetType.type)
} catch (e: IOException) {
throw IllegalArgumentException(
"Conversion failed for $source to ${targetType.name}",
e
)
}
}
}