I am trying to use postman and put these values into the database, but I keep getting an exception.
what im trying to deserialize from postman:
{
"end_date": "2443-11-34 12:43:23",
"start_date": "2443-11-34 12:43:23"
}
The exception that I get:
2020-05-20 10:55:04.572 WARN 4452 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot
deserialize value of type `java.time.Instant` from String "2443-11-34 12:43:23": Failed to deserialize
java.time.Instant: (java.time.format.DateTimeParseException) Text '2443-11-34 12:43:23' could not be
parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot
deserialize value of type `java.time.Instant` from String "2443-11-34 12:43:23": Failed to deserialize
java.time.Instant: (java.time.format.DateTimeParseException) Text '2443-11-34 12:43:23' could not be parsed at index 10
at [Source: (PushbackInputStream); line: 3, column: 17] (through reference chain:
com.project.rushhour.model.post.AppointmentPostDTO["end_date"])]
appointment entity:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Appointment extends BaseEntity {
@NotNull
@DateTimeFormat(style = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
@JsonDeserialize(using = JacksonInstantDeserializer.class)
private Instant startDate;
@NotNull
@JsonDeserialize(using = JacksonInstantDeserializer.class)
@DateTimeFormat(style = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private Instant endDate;
My appointmentDto class:
@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class AppointmentDTO extends BaseDTO {
@JsonProperty("start_date")
private Instant startDate;
@JsonProperty("end_date")
private Instant endDate;
My AppointmentGetDTO class that I use
public class AppointmentGetDTO extends AppointmentDTO {
}
I also have all of the jackson dependencies
My custom deserializer that I use:
public class JacksonInstantDeserializer extends StdDeserializer<Instant> {
public JacksonInstantDeserializer() { this(null); }
public JacksonInstantDeserializer(Class<?> clazz) { super(clazz); }
@Override
public Instant deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {
return Instant.parse(parser.getText());
}
}