I want to send a JSON request body where fields can be enum values. These enum values are in camelCase, but the enum values are UPPER_SNAKE_CASE.

Kotlin classes:

data class CreatePersonDto @JsonCreator constructor (

@JsonProperty("firstName") val firstName: String,

@JsonProperty("lastName") val lastName: String,

@JsonProperty("idType") val idType: IdType

)

enum class IdType {

DRIVING_LICENCE,

ID_CARD,

PASSPORT;

}

My endpoint signature:

@PostMapping

fun createPerson(@RequestBody person: CreatePersonDto)

HTTP request:

curl -d '{ "firstName": "King", "lastName": "Leonidas", "idType": "drivingLicence" }' -H "Content-Type: application/json" -X POST http://localhost:8080/person

I want to convert "drivingLicence" to DRIVING_LICENCE implicitly.

I have tried:

org.springframework.core.convert.converter.Converter: It works for @RequestParam, but not for @RequestBody

org.springframework.format.Formatter: I registered this formatter, but when I make the request the parse() method is not executed.

My configuration so far:

@Configuration

class WebConfig : WebMvcConfigurer {

override fun addFormatters(registry: FormatterRegistry) {

registry.addConverter(IdTypeConverter())

registry.addFormatter(IdTypeFormatter())

}

}

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐