Java is a well-known language for the versatility it offers to programmers. It is a programing language that created a stir in the programming world when it is launched. There are many programs out there that can’t be performed without using Java. As it is a popular language that is frequently used for various programs, you are more likely to get errors, which is really common. You may also encounter the Error “Java 8 date/time type `java.time.Instant` not supported by default”.
The error is common as well, and there is no need to get panicked. You must have looked for the problem, but you are at the right place to get efficient and quick solutions to get rid of the error warning. Let’s have a look at how you encounter the error
How do you encounter the error?
You get the error warning when you are using Date-Time APIs in Java. You may not understand the Java library, java.time.Instant. You must have issues understanding the instant library’s implementation. When you are working on a spring-boot program for creating a RESTful service along with the CRUD operation, that too on MongoDB. You may not get any error warning until you use instant createdOn and updatedOn in DTO.
Have a look at the code where you encounter the error
@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**
}
The DTO response you get must be like this:
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class ResponseDto {
private Integer count;
private Object value;
private Object error;
private Object info;
}
Along with the following 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;
}
When using Lombok plugins, you may get the error
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
Now check out how to solve the issue
How to solve Error “Java 8 date/time type `java.time.Instant` not supported by default”
A few solutions are quite effective when it comes to solving the error warning.
Solution 1 – Dependency in pom.xml
This one is good enough to solve the error. You need to just include com.fasterxml.jackson.datatype in pom.xml. Follow this:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
Solution 2 – The JavaTimeModule Registry
The next possible solution to fix the error “Java 8 date/time type `java.time.Instant` not supported by default” is to register JavaTimeModule. In the case of using com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in the classpath, you need to register only adding the dependency will not work along, you need to also declare a @Bean. You do this using the below code
@Bean
public Module dateTimeModule(){
return new JavaTimeModule();
}
Conclusion
I hope the solutions we have discussed to tackle the error “Java 8 date/time type `java.time.Instant` not supported by default” may help you. Solutions are effective yet simple to solve.
On that note, I wish you luck!
Reference Source: https://www.appsloveworld.com/springboot/100/3/java-8-date-time-type-java-time-instant-not-supported-by-default-issue