-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Description
I'm using default json formatting configuration in my application.properties and expected dates to be returned as long timestamp values.
I've run into an issue using @TeMPOraL(TemporalType.DATE) annotation:
@Temporal(TemporalType.DATE)
@Column(name="dob")
private Date dateOfBirth;
When I create a new entity using a PUT endpoint, the dob date is returned as a long timestamp
as expected. However, a subsequent GET request returns json with date formatted as yyyy-MM-dd
.
In addition, mockmvc test returns json with dob date formatted as long timestamp which masks the actual problem.
I'm using boot spring boot 1.4.0 and java 1.8.
Steps to reproduce:
- git clone --branch feature/date-temporal https://github.com/pavelfomin/spring-boot-rest-example.git
- mvn clean test
- observe that both
getPersonById
andcreatePerson
tests pass confirming that thedateOfBirth
filed returned is a number
- observe that both
- start an application by running
com.droidablebee.springboot.rest.Application
- create a person using the following curl command:
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
"dateOfBirth": "2016-10-17T18:09:52.540Z",
"firstName": "james",
"lastName": "bond",
"middleName": "007"
}' 'http://localhost:8080/v1/person'
- Observe the date format in the response as a long timestamp
{"id":1,"firstName":"james","lastName":"bond","middleName":"007","dateOfBirth":1476727792540}
- Find a person by id returned in the previous
create
response:
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/v1/person/1'
- Observe the date format in the response in a
yyyy-MM-dd
format:
{"id":1,"firstName":"james","lastName":"bond","middleName":"007","dateOfBirth":"2016-10-17"}
If I remove @Temporal
then the date is returned as a timestamp. But it's also stored in the database with the time component. Adding @Temporal
forces hibernate parameter binding for the date field to DATE
instead of TIMESTAMP
causing the the time truncation which is the desired behavior for the dob field.