I having issues when Jackson tries to serialize the LocalDate columns from my classes but the database is completely fine, everytime the JSON displays one day off from the database.
While the database display as intended:
2018-10-01
The JSON output from calling a Spring REST entrypoint:
{ "ticketStart": "2018-09-30" }
It shows an day was lost. The database table doesn't store the time, just the date as intended, The timezones are completely discardable, since my project is for internal use. I tried to google my specific issue and the only i found this and this, but none of them solved my problem.
There's a way to enforce to serialize the dates from my database as-is, (aka: conversion disabled).
Entity Code:
@Entity
@Table(name="tickets")
public class Ticket
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
private long TicketId;
@Column(unique=true)
@Getter
@Setter
private LocalDate TicketStart;
@Column(unique=true)
@Getter
@Setter
private LocalDate TicketEnd;
//.... some fields omitted
}
Since the Tickets entity are generated automatically by a CRON script or specified in a range of dates, where a ticket has the Monday and Sunday of every week:
public Ticket GenerateEmptyTicket(LocalDate begin, LocalDate end)
{
Ticket newTicket = new Ticket();
newTicket.setTicketStart(begin);
newTicket.setTicketEnd(end);
newTicket.setYear(begin.getYear());
newTicket.setMonth(begin.getMonth());
newTicket.setLastMod(new Date());
boolean isLocked = false;
LocalDate date = LocalDate.now();
if(date.isAfter(end)) isLocked = true;
newTicket.setLocked(isLocked);
//@TODO: Set a default responsible in the configuration.
SystemUser rootUser = usersRepo.findByUsername("root").get();
newTicket.setResponsable(rootUser);
return newTicket;
}
public void GenerateTicketsBetweenTwoDates(LocalDate begin, LocalDate end)
{
List<LocalDate> weekDates = new ArrayList<>();
if(begin.getDayOfWeek() != DayOfWeek.MONDAY)
while(begin.getDayOfWeek() != DayOfWeek.MONDAY)
begin = begin.minusDays(1);
LocalDate tmp = begin;
while(tmp.isBefore(end)){
weekDates.add(tmp);
tmp = tmp.plusWeeks(1);
}
if(end.getDayOfWeek() == DayOfWeek.MONDAY)
weekDates.add(end);
LocalDate current = LocalDate.now();
List<Ticket> ticketsToSave = new ArrayList<>();
for(LocalDate dat : weekDates)
{
logger.info("BEGIN DATE:" + dat.toString());
LocalDate __end = dat.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
if(current.isAfter(__end)) logger.info("LOCKED: YES");
ticketsToSave.add(GenerateEmptyTicket(dat, __end));
}
ticketsRepo.saveAll(ticketsToSave);
}
The database and the Jackson serialization displays no errors, just the undesired "day off" result from Serialization.
NOTE: The database JDBC connection (MySQL) specifies useJDBCCompliantTimezoneShift=false&useLegacyDatetimeCode=false&serverTimezone=UTC
from the application.properties spring.datasource.url