Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 date/time type `java.time.Instant` not supported by default Issue : [duplicate]

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

like image 962
Jim Avatar asked Mar 27 '21 12:03

Jim


People also ask

What is an instant in the Java 8 date and time API?

The Java Date Time API was added from Java version 8. instant() method of Clock class returns a current instant of Clock object as Instant Class Object. Instant generates a timestamp to represent machine time. So this method generates a timestamp for clock object.

What is the default format of LocalTime in Java 8?

LocalTime LocalTime is an immutable class whose instance represents a time in the human readable format. It's default format is hh:mm:ss. zzz.

What class is date and time in Java 8?

SimpleDateFormat: It is a class that is used to format and parse the dates in a predefined manner or user defined pattern. java. util.

Is instant in UTC Java?

No, an Instant is not at local time. An Instant by definition is in UTC. So no need for the machinations seen in this Answer trying to get into UTC; the Instant already is in UTC.

Is Java 8 date/time not supported by default?

In this short article, we will present a solution for the Java 8 date/time type java.time.LocalDateTime not supported by default exception. This kind of exception is thrown when we tried to convert an Object with LocalDateTime from Java 8 using ObjectMapper. 2.

Does Java 8 support SQL with Time Zone?

Java 8 has introduced the java.time packages, and the JDBC 4.2 API added support for the additional SQL types TIMESTAMP WITH TIME ZONE and TIME WITH TIME ZONE. We can now map the JDBC Types TIME, DATE, and TIMESTAMP to the java.time types – LocalTime, LocalDate , and LocalDateTime:

What is Java 8 date time API?

Date Time API is one of the biggest features of Java 8 release. Java was missing a consistent approach for Date and Time from start and Java 8 Date Time API is a welcome addition to the core Java APIs. Why do we need new Java Date Time API?

Why is my spring objectmapper not adding the javatimemodule?

The problem there was it was creating a new ObjectMapper instance that was not adding the JavaTimeModule. Here is a sample test that works in Spring 2.4.5 but fails in 2.5.0/2.5.1 with com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.ZonedDateTime not supported by default


1 Answers

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

like image 102
Olivier Boissé Avatar answered Oct 06 '22 00:10

Olivier Boissé