I am implementing a declarative client in Micronaut that looks like this:
@Get("/dostuff{?requestObject*}")
fun getStuff(requestObject: MyRequestObject): String
My MyRequestObject contains an enum that is represented by some string:
data class MyRequestObject(val myEnum: MyEnum)
enum class MyEnum(val stringRep: String) {
AREASONABLENAME("someSillyString");
}
When I now send a request via the client the value from requestObject
generates the following query /?myEnum=AREASONABLENAME
. What I actually need is /?myEnum=someSillyString
.
I tried the following things without any success:
add JsonValue function to MyEnum:
@JsonValue fun getJsonValue() = stringRep
- of course did not help
implement a TypeConverter
for MyEnum
@Singleton
class MyEnumTypeConverter : TypeConverter<MyEnum, String> {
override fun convert(`object`: MyEnum?, targetType: Class<String>?, context: ConversionContext?): Optional<String> {
return Optional.ofNullable(`object`?.stringRep)
}
}
Is there a way to achieve the desired behaviour?
You can override the toString
method in the Enum so that when the converter tries to convert it to a string you can control the result of the operation:
enum class MyEnum(val stringRep: String) {
AREASONABLENAME("someSillyString");
override fun toString(): String {
return stringRep
}
}