Serialization Localdatetime

Asked

Viewed 498 times

0

I am trying to serialize an object that has a date with the Localdatetime class using Spring Boot, but the following error:

2016-10-09 18:28:26.218 WARN 17395 --- [ XNIO-2 task-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Text '2016-10-09T13:00:00.000Z' could not be parsed at index 2 (through reference chain: br.com.wt.agendadoador.modelo.Agenda["date"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Text '2016-10-09T13:00:00.000Z' could not be parsed at index 2 (through reference chain: br.com.wt.agendadoador.modelo.Agenda["date"])

I did some research but I couldn’t find the reason...

Class stretch:

@Entity
public class Agenda {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="doador_id")
@JsonProperty
private Doador doador;
@Enumerated(EnumType.STRING)
private StatusAgenda statusAgenda;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="laboratorio_id")
@JsonProperty
private Laboratorio laboratorio;
@JsonFormat(pattern = "dd-MM-yyyy HH:mm:ss")
@DateTimeFormat(iso = DateTimeFormat.ISO.TIME)
private LocalDateTime date;

My setup on application.properties and Jackson-datetype-jrs310:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS =false
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

Finally the Controller `

@RestController
@RequestMapping(value = "agenda")
public class AgendaController {

@Autowired
private AgendaRepository agendaRepository;

@RequestMapping(value = "/", method = RequestMethod.POST,headers="Accept=application/json", produces = "application/json")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
public ResponseEntity<Void> add(@Valid @RequestBody Agenda agenda) {
    HttpHeaders headers = new HttpHeaders();
    try {
        agenda.setStatusAgenda(StatusAgenda.EMABERTO);
        agendaRepository.save(agenda);
        return new ResponseEntity<Void>(headers, HttpStatus.OK);
    } catch (RuntimeErrorException e) {
        System.out.println(e.getMessage());
        return new ResponseEntity<Void>(headers, HttpStatus.NOT_ACCEPTABLE);
    }

}`

Thank you

1 answer

1


I believe you should put the Annotation @JsonDeserialize on the field date of your class Agenda to use this deserializer only for this field. If you want to register the deserializer globally so that every field of the type LocalDateTime use this deserializer, then you should do so:

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new ItemDeserializer());
mapper.registerModule(module);

As you must be using spring-boot, you have to configure the module in mapper already created by auto-configuration, I usually do this way:

@Configuration
public class JacksonConfiguration {

    @Autowired
    private void registerSerializersDeserializers(List<ObjectMapper> objectMappers) {
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());

        objectMappers.forEach(objectMapper -> objectMapper.registerModule(simpleModule));
    }
}

I hope it helps.

  • After applying the setting Warn no longer shows, but now the object is null. Have any idea what it might be?

  • So off the top of my head, I’d venture it could be the shape of your date. I would remove the format Annotations on top of the attribute and pass the json in the default formatting which is iso-8601.

  • Really the date is coming wrong so it was not Rializando. Thanks for the force.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.