Pihentagy
03/16/2023, 1:40 PMpublic enum class Gender(@JsonValue val value: String) {
MALE("m"), FEMALE("f");
}
The controller:
@GetMapping("/test", produces = [org.springframework.http.MediaType.APPLICATION_JSON_VALUE])
fun gender(@RequestParam gender: Gender): Map<String, Gender> =
when (gender) {
Gender.MALE -> mapOf("result" to Gender.FEMALE)
Gender.FEMALE -> mapOf("result" to Gender.MALE)
}
For GET localhost:8080/test?gender=m
I expect to get {"result": "f"}
.Thomas
03/16/2023, 1:45 PM@JsonProperty("f")
is the way2go. https://www.baeldung.com/jackson-serialize-enumsJacob
03/16/2023, 1:46 PM@get:JsonValue
Thomas
03/16/2023, 1:47 PMPihentagy
03/16/2023, 1:51 PMPihentagy
03/16/2023, 1:55 PMThomas
03/16/2023, 1:58 PMPihentagy
03/16/2023, 1:59 PMThomas
03/16/2023, 2:01 PM@get:JsonProperty("m")
MALE("m"),
Something like this?Pihentagy
03/16/2023, 2:05 PMJacob
03/16/2023, 2:06 PMPihentagy
03/17/2023, 8:21 AMefemoney
03/18/2023, 4:50 PMPihentagy
03/22/2023, 3:34 PMenum class VoltageLevel(@get:JsonValue val s: String) {
HIGH("h"),
MEDIUM("m"),
LOW("l")
}
I use it in the following controller:
@RestController
class MapController(private val mapElementRepository: MapElementRepository) {
@GetMapping("/map-elements/view-window", produces = [MediaType.APPLICATION_JSON_VALUE])
fun index(
// other parameters
@RequestParam voltageLevels: Array<VoltageLevel>?
): MapElementResult {
// ...
}
}
On the UI, the values are ok (h, m and v), but when I fire a request, I get
Could not resolve parameter [5] in public foobar.mapservice.MapElementResult foobar.mapservice.MapController.index(java.math.BigDecimal,java.math.BigDecimal,java.math.BigDecimal,java.math.BigDecimal,foobar.mapservice.NetworkType[],foobar.mapservice.VoltageLevel,int): Failed to convert value of type 'java.lang.String' to required type 'foobar.mapservice.VoltageLevel'; Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam foobar.mapservice.VoltageLevel] for value 'h'
Same error if I try this way:
enum class VoltageLevel {
@JsonProperty("h")
HIGH,
@JsonProperty("m")
MEDIUM,
@JsonProperty("l")
LOW
}
efemoney
03/22/2023, 3:45 PMConversionService
and then try to convert a string from “h” to VoltageLevel.
If that fails then you have confirmation that is the issue. And then you can supply your own Converter<T, U>
to Spring and it will use your custom converter any times it needs to convert from String to VoltageLevelPihentagy
03/22/2023, 4:41 PMenum class VoltageLevel(@get:JsonValue val value: String) {
HIGH("h"), MEDIUM("m"), LOW("l")
}
@Component
class StringToVoltageLevel : Converter<String, VoltageLevel> {
override fun convert(source: String): VoltageLevel? {
return VoltageLevel.values().find { it.value == source }
}
}
Is it possible to generalize it? Say I have some other enums, which also have a string value, and to handle the converting with one converter.Pihentagy
03/22/2023, 5:29 PM@Component
public class StringToEnumConverterFactory<E : Enum<*>> : ConverterFactory<String, Enum<*>> {
private class StringToEnumConverter<T : Enum<T>>(val enumType: Class<T>) :
Converter<String, T> {
override fun convert(source: String): T {
when (enumType) {
is CustomEnum<*> -> enumType.value
else -> java.lang.Enum.valueOf(enumType, source.trim())
}
}
}
override fun <T : Enum<*>> getConverter(targetType: Class<T>): Converter<String, T> {
return StringToEnumConverter(targetType);
}
}