My frontend vue application sends a luxon DateTime object to a Spring Boot backend which has a LocalDateTime field, however, java is unable to parse the DateTime object. I have tried using both luxon's toSQL() and toISO() methods to format, but of no avail.
What would be the optimal way of sending date time from a vue application to a java backend?
example request:
"date_deadline": "2024-03-07 14:54:51.440 +08:00"
The java entity looks as follows:
@Entity
@Table(name = "todos")
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private LocalDateTime date_created;
@Column (name = "date_deadline")
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime date_deadline;
}
When passing the json in the request body, I get the following 400 response:
"message": "JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String \"2024-03-07 14:54:51.440 +08:00\": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2024-03-07 14:54:51.440 +08:00' could not be parsed at index 10",
You are having this problem because the date format that Vue sends is not compatible with the format that LocalDateTime has in Java, which is ISO-8601 ("2024-03-07T14:54:51.440"). You can solve this in several ways:
Send the date as String. You can convert the date in Vue to a String with the ISO-8601 format and send it to the backend.
Custom Deserialization in Spring Boot. You can modify the default deserializer to fit your format.
You can use it with the annotation
@JsonDeserializein your entity.