JDK 17Spring Boot 无法创建专用java.time.LocalDateTime

nc1teljy  于 2022-11-20  发布在  Java
关注(0)|答案(3)|浏览(622)

我在简单的mongodb实体中有私有的LocalDateTime lastModifiedDate属性。使用最新的2.5.5和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

甚至我删除了lombok,所以我们应该再次回滚到Java 1. 8?:)

nxowjjhe

nxowjjhe1#

据我所知,我们需要添加以下Java OPTs参数:

- JVM_OPTS=  --add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.management/javax.management=ALL-UNNAMED --add-opens=java.naming/javax.naming=ALL-UNNAMED

例如我在Docker compose中添加的。
我想很快Sping Boot /JakartaEE就会升级他们的配置。

1bqhqjot

1bqhqjot2#

有相同的问题。不要使用gsonMap器。从www.example.com文件中删除下一行application.properties。

spring.mvc.converters.preferred-json-mapper=gson
2lpgd968

2lpgd9683#

我排除了gson的第一个并添加了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;
    }
}

相关问题