I read a few questions with answers here in SO regarding serialization and deserialization between java.time.LocalDateTime
and the JSON property, but I can't get it to work.
I managed to configure the Spring boot application to return dates in the desired format ( YYY-MM-dd HH:mm
), but I have problems accepting the values ββin this format in JSON.
This is all I have done so far:
Added maven dependency for jsr310
:
<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
Indicated by jsr310
in my main class:
@EntityScan(basePackageClasses = { App.class, Jsr310JpaConverters.class })
Disable serialization as timestamps in application.properties
:
spring.jackson.serialization.write_dates_as_timestamps=false
And this is my entity mapping for datetime:
@Column(name = "start_date") @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) @JsonFormat(pattern = "YYYY-MM-dd HH:mm") private LocalDateTime startDate;
In my database, I store this date as TIMESTAMP in the following format: 2016-12-01T23:00:00+00:00
.
If I access this object through my controller, it returns JSON with the correct startDate format. When I try to publish it and deserialize it using the format YYYY-MM-dd HH:mm
, I get the following exception:
{ "timestamp": "2016-10-30T14:22:25.285+0000", "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "Could not read document: Can not deserialize value of type java.time.LocalDateTime from String \"2017-01-01 20:00\": Text '2017-01-01 20:00' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MonthOfYear=1, WeekBasedYear[WeekFields[SUNDAY,1]]=2017, DayOfMonth=1},ISO resolved to 20:00 of type java.time.format.Parsed\n at [Source: java.io.PushbackInputStream@679a734d ; line: 6, column: 16] (through reference chain: com.gigsterous.api.model.Event[\"startDate\"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.time.LocalDateTime from String \"2017-01-01 20:00\": Text '2017-01-01 20:00' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MonthOfYear=1, WeekBasedYear[WeekFields[SUNDAY,1]]=2017, DayOfMonth=1},ISO resolved to 20:00 of type java.time.format.Parsed\n at [Source: java.io.PushbackInputStream@679a734d ; line: 6, column: 16] (through reference chain: com.gigsterous.api.model.Event[\"startDate\"])", "path": "/api/events" }
I know that there are many answers on this subject, but after them and attempts for several hours did not help me figure out what I was doing wrong, so I would be glad if someone could tell me what Iβm missing, Thanks for any input on this!
EDIT: These are all the classes involved in the process:
Repository:
@Repository public interface EventRepository extends PagingAndSortingRepository<Event, Long> { }
Controller:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Event> createEvent(@RequestBody Event event) { return new ResponseEntity<>(eventRepo.save(event), HttpStatus.CREATED); }
My JSON requests payalod:
{ "name": "Test", "startDate": "2017-01-01 20:00" }
Event:
@Entity @Table(name = "events") @Getter @Setter public class Event { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "event_id") private long id; @Column(name = "name") private String name; @Column(name = "start_date") @DateTimeFormat(iso = DateTimeFormat.ISO.TIME) @JsonFormat(pattern = "YYYY-MM-dd HH:mm") private LocalDateTime startDate; }