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
After applying the setting Warn no longer shows, but now the object is null. Have any idea what it might be?
– cido18
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.
– Fábio Zoz
Really the date is coming wrong so it was not Rializando. Thanks for the force.
– cido18