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