Весенняя загрузка JDK 17 Невозможно сделать частным java.time.LocalDateTime

У меня есть частное свойство LocalDateTime lastModifiedDate в простой сущности mongodb. Запустите приложение с последней версией 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

Даже ломбок я удалил, так что надо снова откатиться на Java 1.8?:)

3 ответа

Откат не требуется, просто удалил мою конфигурацию mongo по умолчанию, посмотрю позже

      @Configuration
public class MongoConfiguration {

    @Autowired
    private MongoDatabaseFactory mongoDbFactory;

    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new DateToZonedDateTimeConverter());
        converters.add(new ZonedDateTimeToDateConverter());
        return new MongoCustomConversions(converters);
    }

    @Bean
    public MongoTemplate mongoTemplate() throws UnknownHostException {
        MappingMongoConverter converter = new MappingMongoConverter(
                new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());
        converter.setCustomConversions(customConversions());
        converter.afterPropertiesSet();
        return new MongoTemplate(mongoDbFactory, converter);
    }

    class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            return !Optional.ofNullable(source).isPresent() ? null : ofInstant(source.toInstant(), systemDefault());
        }
    }

    class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        @Override
        public Date convert(ZonedDateTime source) {
            return !Optional.ofNullable(source).isPresent() ? null : Date.from(source.toInstant());
        }
    }
}

Я исключил gson первым и добавил маппер:

      @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;
    }
}

Есть такая же проблема. Не используйте gson mapper. Удалите следующую строку из файла application.properties.

      spring.mvc.converters.preferred-json-mapper=gson
Другие вопросы по тегам