调用mongo存储库的save方法时未调用Mongo Date自定义转换器

时间:2019-10-24 08:43:35

标签: mongodb spring-boot java-8 spring-data-mongodb

我正在使用Linux Debian9。我已经安装了JDK 1.8。我正在使用maven 3.6版,而springboot的版本是2.1。 mongodb版本是3.6。

下面是我要保存在mongodb中的Java中的类模型:

@org.springframework.data.mongodb.core.mapping.Document(collection = FileContentIndexQueue.ENTITY_COLLECTION_NAME)
@CompoundIndexes({
    @CompoundIndex(name = "state_timestamp", def = "{'state' : 1, 'timestamp': -1}")
})
@QuerySupertype
public class FileContentIndexQueue extends AbstractEntityNoLock {
        ...

    private ZonedDateTime timestamp;


    public FileContentIndexQueue() {
    }

    public FileContentIndexQueue(String fileId, String parentId, String childType, String indexName) {
        super();
        this.fileId = fileId;
        this.parentId = parentId;
        this.childType = childType;
        this.indexName = indexName;
        this.state = IndexingState.TODO;
        this.timestamp = ZonedDateTime.now();
    }

        ...

    public ZonedDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(ZonedDateTime timestamp) {
        this.timestamp = timestamp;
    }


}

我正在使用spring数据mongodb,下面是存储库类和自定义存储库类及其实现:

//Repository 
public interface FileContentIndexQueueRepositoryMongoElastic extends 
                MongoElasticRepository<FileContentIndexQueue, String>
                , FileContentIndexQueueRepositoryCustom 
{
}


//Custom Repository
public interface FileContentIndexQueueRepositoryCustom {
    void addFileWithContentToExtract(AbstractEntityNoLock entity, IFile file) throws CommonAllegatiException;
    void removeFileWithContentToExtract(AbstractEntityNoLock entity, IFile file) throws CommonAllegatiException;    
}

//Custom Repository Class Implementation
public class FileContentIndexQueueRepositoryCustomImpl implements FileContentIndexQueueRepositoryCustom {


    @Autowired
    @Lazy
    private FileContentIndexQueueRepositoryMongoElastic fileContentIndexQueueRepositoryMongoElastic;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addFileWithContentToExtract(AbstractEntityNoLock entity, IFile file) throws CommonAllegatiException {
        if(entity.getId() == null) {
            throw new CommonAllegatiException("Impossibile creare il FileContentIndexQueue da una entity senza id quindi non ancora salvata");
        }
        if(file == null) {
            throw new CommonAllegatiException("Impossibile creare il FileContentIndexQueue da un IFile null");
        }
        if(file.getFileId() == null) {
            throw new CommonAllegatiException("Impossibile creare il FileContentIndexQueue da un IFile senza id");
        }
        //da ricavare dalla entity
        String parentId = entity.getId();
        String indexName = elasticsearchTemplate.getPersistentEntityFor(entity.getClass()).getIndexName();
        String fileId = file.getFileId();

        FileContentIndexQueue fciq = new FileContentIndexQueue(fileId, parentId, CHILDTYPE, indexName);
        fileContentIndexQueueRepositoryMongoElastic.save(fciq); 
          //**after the save is the point where the error is generated**
    }

    @Override
    public void removeFileWithContentToExtract(AbstractEntityNoLock entity, IFile file) throws CommonAllegatiException {
              ...
    }   

}


以上所有类都是库的一部分,我已使用maven从公司资源库中下载了该库,但无法修改。问题在于FileContentIndexQueue.java模型具有类型为ZonedDateTime的属性时间戳,而mongo db不支持该类型,并且spring数据没有内置转换器,并引发错误: org.bson.codecs.configuration。 CodecConfigurationException:找不到类java.time.ZonedDateTime的编解码器。

下面也是应用程序属性文件,该文件具有我为mongo db和弹性搜索设置的属性:

 #MongoDB
spring.data.mongodb.uri=mongodb://localhost:27017/?safe=true&w=1
spring.data.mongodb.database=operaTestDb

 #Elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.mongoelastic.save-on-elastic=true

我尝试创建客户转换器并注册调用mongo存储库的save方法时要调用的转换器。下面是我已实现的解决方案的代码。

@Configuration
public class ConverterConfig  {

    @Autowired
    MongoDbFactory mongoDbFactory;


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


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

    enum DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        INSTANCE;

        @Override
        public ZonedDateTime convert(Date source) {
            return ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }

    enum ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        INSTANCE;

        @Override
        public Date convert(ZonedDateTime source) {
            return Date.from(source.toInstant());
        }
    }
}


问题甚至是当我创建转换器并注册它们时出现的错误: org.bson.codecs.configuration.CodecConfigurationException:找不到类java.time.ZonedDateTime的编解码器。仍然存在。我在转换器中放了一个调试程序,但是没有到达那里。就像转换器根本没有注册一样。我将对我应该怎么做进行调试,或者如果您不使用转换器就此问题有其他解决方案的任何建议,不胜感激。不能将模型属性从ZonedDatetime修改为其他日期格式,因为我无权访问该库。

亲切的问候, 兰多。

2 个答案:

答案 0 :(得分:0)

通过在ConverterConfig类中进行以下修改解决了此问题:

  1. 从方法customConversions()中删除bean批注
  2. 从转换方法中删除替代注释
  3. 在DateToZonedDateTimeConverter枚举中添加@ReadingConverter批注
  4. 在ZonedDateTimeToDateConverterenum中添加@WritingConverter注释

下面是对我有用的ConverterConfig类的版本。我希望它能帮助您不要像我一样浪费时间。

@Configuration
public class ConverterConfig {
    @Autowired
    MongoDbFactory mongoDbFactory;
    @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);
    }
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(DateToZonedDateTimeConverter.INSTANCE);
        converters.add(ZonedDateTimeToDateConverter.INSTANCE);
        return new MongoCustomConversions(converters);
    }
    @ReadingConverter
    enum DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
        INSTANCE;
        public ZonedDateTime convert(Date source) {
            return source == null ? null : ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }
    @WritingConverter
    enum ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, LocalDateTime> {
        INSTANCE;
        public LocalDateTime convert(ZonedDateTime source) {
            return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }
}

答案 1 :(得分:0)

我花了一个小时才弄清最新版本的spring数据mongo,应该使用org.bson.Document代替com.mongodb.BasicDBObject。这是一个示例:

@Component
@WritingConverter
public class UserModelConverter implements Converter<UserModel, Document> {

    @Override
    public Document convert(UserModel s) {
        Document obj = new Document();
        obj.put("firstName", "FirstName");
        obj.put("lastName", "LastName");

        obj.remove("_class");

        return obj;
    }
}