As suggest the title I'm trying to figure out why in json responses from my spring boot backend, date format are differents between the get and the post/put requests performed on the same object type. As I can see the problems with date formats are quite common, but I don't really understand why this happens here since the model and the entity are the same (so date patterns should be the same as well, I guess) for both method and the database connection is configured with the correct timezone. Maybe JPA is the cause? But why should it behave differently if have not specified any JPA configuration?
Workstation bean:
public class Workstation {
private Integer idWorkstation;
private Integer idResource;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date workstationDate;
private String freeName;
private String codeWorkstation;
}
Workstation entity:
Entity
@Table(name = "workstations")
public class WorkstationEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_workstation" , scale = 11)
private Integer idWorkstation;
@Column(name = "id_resource", scale = 11)
private Integer idResource;
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd-MM-yyyy")
@Column(name = "workstation_date", nullable = false)
private Date workstationDate;
@Column(name = "free_name", length = 255)
private String freeName;
@Column(name = "code_workstation", length = 3)
private String codeWorkstation;
A controller class simply delegate the service class to perform the get/insert/update on db through the repository, mapping the beans to the entities and vice-versa. There aren't any date conversions through the whole "process".
Repository (for insert/update I'm using the default "save" method):
public interface WorkstationRepository extends JpaRepository<WorkstationEntity, Integer> {
List<WorkstationEntity> findAllByWorkstationDate(Date date);
....
}
Date types are "java.util.Date" for all classes and the connection with the db in the application.properties file is configured like this:
jdbc:mysql://address_of_server?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Europe/Rome&zeroDateTimeBehavior=convertToNull
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.jdbc.time_zone = Europe/Rome
spring.jmx.enabled=false
I'm not using any external dependencies for dates management and here's the problem. This is the json response for a post request:
{
"idWorkstation": 3850,
"idResource": 276,
"workstationDate": "2020-10-19T22:00:00.000+0000",
"freeName": null,
"codeWorkstation": "200"
}
While this is the "get" response on the exact same object :
{
"idWorkstation": 3850,
"idResource": 276,
"workstationDate": "2020-10-20",
"freeName": null,
"codeWorkstation": "200"
}
As you can see the date fields are in a different format and, since I'm working with the date part only, I'm having trouble on the front-end side because I'm basically "losing" a day. The "correct" result is the one from the get request, with the date pattern "yyyy/MM/dd" without the TimeZone. Even on the database I can see (through MySQL workbench) that dates are saved with "yyyy/MM/dd" pattern so I can't really see where this misbehavior with post/put requests is occurring. Any guess?