首页 > 解决方案 > 从反应式 GridFS 读取文件时出现 StackOverflowError

问题描述

我对 ReactiveGridFsTemplate 有疑问。我正在尝试读取使用旧 GridFS (com.mongodb.gridfs) 而不是新 GridFS (com.mongodb.client.gridfs.model.GridFS) 编写的 GridFS 文件,并将 UUID 作为 ID 而不是 ObjectId。读取 GridFS 文件元信息很顺利,但只要我想获得 ReactiveGridFsResource,它就会出现一个不错的新 MongoGridFSException(“用于此 GridFS 文件的自定义 id 类型”)。

罪魁祸首是下面来自 ReactiveGridFsTemplate 的代码,它调用 getObjectId() 而不是 getId()。它应该调用此方法还是可以将其重写为 getId() 方法?

    public Mono<ReactiveGridFsResource> getResource(GridFSFile file) {
        Assert.notNull(file, "GridFSFile must not be null!");
        return Mono.fromSupplier(() -> {
            GridFSDownloadStream stream = this.getGridFs().openDownloadStream(file.getObjectId());
            return new ReactiveGridFsResource(file, BinaryStreamAdapters.toPublisher(stream, this.dataBufferFactory));
        });
    }

我破解了 ReactiveGridFsTemplate 以使用 getId() 而不是 getObjectId() 但现在它给了我一个 stackoverflow 异常。有人可以告诉我我做错了什么吗?

        ReactiveGridFsTemplate reactiveGridFsTemplate = new ReactiveGridFsTemplate(mongoDbDFactory, operations.getConverter(), "nl.loxia.collectie.buitenlandbladen.dgn", 1024) {
            public Mono<ReactiveGridFsResource> getResource(GridFSFile file) {
                Assert.notNull(file, "GridFSFile must not be null!");
                return Mono.fromSupplier(() -> {
                    GridFSDownloadStream stream = this.getGridFs().openDownloadStream(file.getId());
                    return new ReactiveGridFsResource(file, BinaryStreamAdapters.toPublisher(stream, this.dataBufferFactory));
                });
            }            
        };

        var q = Query.query((Criteria.where("_id").is("5449d9e3-7f6d-47b7-957d-056842f190f7")));

        List<DataBuffer> block = reactiveGridFsTemplate
                .findOne(q)
                .flatMap(reactiveGridFsTemplate::getResource)
                .flux()
                .flatMap(ReactiveGridFsResource::getDownloadStream)
                .collectList()
                .block();

堆栈跟踪:https ://gist.github.com/nickstolwijk/fa77681572db1d91941d85f6c845f2f4

此外,此代码由于 stackoverflow 异常而挂起。那是对的吗?

标签: spring-data-mongodbproject-reactormongodb-java

解决方案


推荐阅读