I am working with OffsetDateTime objects.
I want to output this type in ISO format so I've added the aforementioned property to my application.yml and it works fine when I use it in my controller.
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Schedule
{
private OffsetDateTime time;
private String mode;
}
Using in my controller:
public ResponseEntity taskManagerTest() {
Schedule bpTaskManagerRequest = new Schedule();
return ResponseEntity.status(HttpStatus.CREATED).headers(null).body(bpTaskManagerRequest);
}
Example result when I am returning my object:
{
"time": "2017-11-12T15:03:05.171Z",
"mode": "eSetTime"
}
But if I use the same object to send it further using RestTemplate in my Spring service:
HttpEntity<Schedule> httpEntity = new HttpEntity<>(bpTaskManagerRequest, headers);
ResponseEntity<String> answer = restTemplate.exchange(bpTaskManagerURL, HttpMethod.POST, httpEntity,
String.class);
It is serialized as:
{
"time": 1510498985.171000000,
"mode": "eSetTime"
}
My RestTemplate is defined as:
@Autowired
private RestTemplate restTemplate;
application.yml snippet:
spring:
jackson:
serialization:
write-dates-as-timestamps: false
build.gradle snippet:
buildscript {
ext {
springBootVersion = '1.5.4.RELEASE'
ext.kotlin_version = '1.1.51'
}
}
compile('com.fasterxml.jackson.module:jackson-module-parameter-names')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jdk8')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
Example project: https://github.com/deepres/OffsetDateTime-with-RestTemplate