Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

534
Visualizações
How to custom convert String to enum in @RequestBody?

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())
    }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can try to use JsonProperty on enum directly

enum IdType {

    @JsonProperty("drivingLicence")
    DRIVING_LICENCE,

    @JsonProperty("idCard")
    ID_CARD,

    @JsonProperty("passport")
    PASSPORT;
}

If you want to have multimapping the simple thing would be defining mapping and using JsonCreator on enum level:

enum IdType {

    DRIVING_LICENCE,
    ID_CARD,
    PASSPORT;

    private static Map<String, IdType> mapping = new HashMap<>();

    static {
        mapping.put("drivingLicence", DRIVING_LICENCE);
        mapping.put(DRIVING_LICENCE.name(), DRIVING_LICENCE);
        // ...
    }

    @JsonCreator
    public static IdType fromString(String value) {
        return mapping.get(value);
    }
}

See also:

  • Deserializing an enum with Jackson
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda