Serialization LocalDateTime

0

I'm 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 could not find the reason ...

Class excerpt:

@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 in 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

    
asked by anonymous 10.10.2016 / 11:07

1 answer

1

I believe you should put annotation @JsonDeserialize on top of the date field of your Agenda class to use this deserializer just for this field. If you want to register deserializer globally so that every field of type LocalDateTime uses 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 should be using spring-boot, you have to configure the module in the mapper already created by auto-configuration, I usually do this:

@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.

    
11.10.2016 / 15:11