JDK 17 spring boot Unable to make private java.time.LocalDateTime

Issue

I have private LocalDateTime lastModifiedDate property in the simple mongodb entity.
Run application with latest 2.5.5 and openjdk 17

Following exeptions I have with date time
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make private java.time.LocalDateTime(java.time.LocalDate,java.time.LocalTime) accessible: module java.base does not "opens java.time" to unnamed module @61baa894
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) ~[na:na]
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[na:na]
    at java.base/java.lang.reflect.Constructor.checkCanSetAccessible(Constructor.java:188) ~[na:na]
    at java.base/java.lang.reflect.Constructor.setAccessible(Constructor.java:181) ~[na:na]
    at org.springframework.util.ReflectionUtils.makeAccessible(ReflectionUtils.java:202) ~[spring-core-5.3.10.jar!/:5.3.10]
    at org.springframework.data.mapping.PreferredConstructor.<init>(PreferredConstructor.java:64) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.model.PreferredConstructorDiscoverer$Discoverers.buildPreferredConstructor(PreferredConstructorDiscoverer.java:222) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.model.PreferredConstructorDiscoverer$Discoverers.access$200(PreferredConstructorDiscoverer.java:91) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.model.PreferredConstructorDiscoverer$Discoverers$1.discover(PreferredConstructorDiscoverer.java:134) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.model.PreferredConstructorDiscoverer.discover(PreferredConstructorDiscoverer.java:79) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.model.BasicPersistentEntity.<init>(BasicPersistentEntity.java:112) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity.<init>(BasicMongoPersistentEntity.java:77) ~[spring-data-mongodb-3.2.5.jar!/:3.2.5]
    at org.springframework.data.mongodb.core.mapping.MongoMappingContext.createPersistentEntity(MongoMappingContext.java:91) ~[spring-data-mongodb-3.2.5.jar!/:3.2.5]
    at org.springframework.data.mongodb.core.mapping.MongoMappingContext.createPersistentEntity(MongoMappingContext.java:40) ~[spring-data-mongodb-3.2.5.jar!/:3.2.5]
    at org.springframework.data.mapping.context.AbstractMappingContext.doAddPersistentEntity(AbstractMappingContext.java:387) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:365) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at java.base/java.util.Collections$SingletonSet.forEach(Collections.java:4905) ~[na:na]
    at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.createAndRegisterProperty(AbstractMappingContext.java:568) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.AbstractMappingContext$PersistentPropertyCreator.doWith(AbstractMappingContext.java:526) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:710) ~[spring-core-5.3.10.jar!/:5.3.10]
    at org.springframework.data.mapping.context.AbstractMappingContext.doAddPersistentEntity(AbstractMappingContext.java:401) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:365) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:258) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:201) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:87) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:73) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getEntityInformation(MongoRepositoryFactory.java:160) ~[spring-data-mongodb-3.2.5.jar!/:3.2.5]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:134) ~[spring-data-mongodb-3.2.5.jar!/:3.2.5]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:319) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:323) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:119) ~[spring-data-mongodb-3.2.5.jar!/:3.2.5]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.10.jar!/:5.3.10]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.10.jar!/:5.3.10]
    ... 50 common frames omitted

Even I removed lombok, so we should rollback again to Java 1.8?:)

Solution

I excluded gson the first
and added mapper:

@Configuration
public class JacksonOffsetDateTimeMapper {

    @Primary
    @Bean
    public ObjectMapper objectMapper() {

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        SimpleModule simpleModule = new SimpleModule();

        mapper.registerModule(simpleModule);
        return mapper;
    }
}

Answered By – Armen Arzumanyan

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.