Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

538
Views
¿Cómo convertir String a enumeración personalizada en @RequestBody?

Quiero enviar un cuerpo de solicitud JSON donde los campos pueden ser valores de enumeración. Estos valores de enumeración están en camelCase, pero los valores de enumeración son UPPER_SNAKE_CASE.

Clases de Kotlin:

 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; }

Mi firma de punto final:

 @PostMapping fun createPerson(@RequestBody person: CreatePersonDto)

Solicitud HTTP:

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

Quiero convertir "permiso de conducir" a PERMISO DE CONDUCIR implícitamente.

Yo he tratado:

  • org.springframework.core.convert.converter.Converter : funciona para @RequestParam , pero no para @RequestBody
  • org.springframework.format.Formatter : registré este formateador, pero cuando realizo la solicitud, el método parse() no se ejecuta.

Mi configuración hasta ahora:

 @Configuration class WebConfig : WebMvcConfigurer { override fun addFormatters(registry: FormatterRegistry) { registry.addConverter(IdTypeConverter()) registry.addFormatter(IdTypeFormatter()) } }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puede intentar usar JsonProperty en enumeración directamente

 enum IdType { @JsonProperty("drivingLicence") DRIVING_LICENCE, @JsonProperty("idCard") ID_CARD, @JsonProperty("passport") PASSPORT; }

Si desea tener mapeo múltiple, lo simple sería definir el mapeo y usar JsonCreator en el nivel de enumeración:

 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); } }

Ver también:

  • Deserializar una enumeración con Jackson
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!