Issue
I am having a tough time understanding java.time.Instant library. I am not sure where there is not a straightforward solution way to implement Instant library. I have a spring-boot project where I have created RESTful service with CRUD operation on MongoDB. Everything was ok until I introduced Instant updatedOn & Instant createdOn in DTO.
Here sample code of my POST response where failure is occurring.
@Override
public ResponseEntity<ResponseDto> create(@RequestBody CLMDto CLMDto) {
CLMDto createdCLMDto =
CLMService.create(CLMDto);
return ResponseEntity.status(HttpStatus.CREATED)
.header(
RESPONSE_HEADER_LOCATION, CLM_URL + "/" + createdCLMDto.getId())
.body(ResponseDto.builder().value(createdCLMDto).build()); //--------------------->**Failing here**
}
My response DTO looks something like this
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class ResponseDto {
private Integer count;
private Object value;
private Object error;
private Object info;
}
and CLMDto
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AdminDataDto {
private Instant createdOn;
private Instant updatedOn;
@JsonProperty(value = "updatedByName", access = JsonProperty.Access.READ_ONLY)
private String updatedByName;
}
In debugger, i noticed place where exception is happening variable createdCLMDto has following values
createdOn:Instant@193 "2021-03-27T11:53:24.774765300Z"
updatedByName:test
updatedOn:Instant@195 "2021-03-27T11:53:24.774765300Z"
as I am using Lombok plugins so I can't debug inside and find the root cause on autogenerated code. I am not looking for a solution but a suggestion where things can go wrong here. My exact error is
Type definition error: [simple type, class java.time.Instant]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Java 8 date/time type `java.time.Instant` not supported by default: add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jsr310\" to enable
handling (through reference chain:
com.abc.service.dto.ResponseDto[\"value\"]->com.abc.dto.AdminDataDto[\"createdOn\"])",
Needless to say but I already tried the solution provided here to updated maven libraries but no success. I find many questions on StackOverflow around this library but no one is explaining the easiest way to implement this libarary.
I have already following dependency in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
I have already tried the solution provided here Java 8 date time types serialized as object with Spring Boot
Solution
Do you have the following artifact into your project ?
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
According to the spring source code, if the class com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
is present in your classpath, the JavaTimeModule
should be registered
Answered By - Olivier Boissé
Answer Checked By - Mildred Charles (JavaFixing Admin)