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

290
Views
Convertir la fecha de la cadena Json en ZoneDateTime en Spring 4 MVC

Puedo obtener ZoneDateTime llamando a Spring Rest API. La fecha que obtengo en json tiene el siguiente formato:

 { "2017-04-24T15:13:06-05:00" }

Pude lograr esto en Spring 4 MVC configurando el siguiente código en ApplicationConfiguration.class:

 @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.registerModule(new JavaTimeModule()); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); converter.setObjectMapper(objectMapper); converters.add(converter); }

Ahora, cuando quiero enviar esa fecha json a Spring Rest para la operación posterior. Recibo la siguiente excepción:

 WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.time.ZonedDateTime from String value ("2017-04-24T15:13:06-05:00"): Text '2017-04-24T15:13:06-05:00' could not be parsed at index 19 nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.time.ZonedDateTime from String value ("2017-04-24T15:13:06-05:00"): Text '2017-04-24T15:13:06-05:00' could not be parsed at index 19

Intenté usar CustomDeserialization.class y anotar el campo ZoneDateTime con @JsonDeserialize(CustomDeserialization.class) pero esto tampoco funciona.

¿Cuál es la mejor manera de convertir json con fecha en ZoneDateTime en Spring 4 MVC?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si desea enviar una fecha usando JSON, creo que la forma más sencilla es convertir primero la fecha en tipo largo y luego enviarla como JSON. Algo como esto:

 public class MyJson { Long date; public MyJson() { } public MyJson(Long date) { this.date = date; } public Long getDate() { return date; } public void setDate(Long date) { this.date = date; } }

y en el método principal:

 Date date = new Date(); MyJson json = new MyJson(date.getTime()); ObjectMapper objectMapper = new ObjectMapper(); String strJson = objectMapper.writeValueAsString(json); MyJson result = objectMapper.readValue(strJson, MyJson.class);
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!