Spring MongoRepository#findall:ConverterNotFoundException

Spring MongoRepository#findall: ConverterNotFoundException

我有两个简单的文档MyDocNestedDoc

MyDoc:

public class MyDoc {

    @Id
    private final String id;
    private final NestedDoc nested;


    public MyDoc (MyIdentifier myIdentifier, Nested nested) {

        this(myIdentifier.toString(), 
                new NestedDoc(nested.getIdentifier(), nested.getStp()));

    }

    @PersistenceConstructor
    public MyDoc (String id, NestedDoc nestedDoc) {
        this.id = id;
        this.nestedDoc = nestedDoc;
    }

    // ...

}

NestedDoc:

public class NestedDoc {

    private final String identifier;
    private final Stp stp; // is an enum    

    @PersistenceConstructor
    public NestedDocDoc (String identifier, Stp stp) {
        this.identifier = identifier;
        this.stp = type;
    }

    // ...

}

有一个直接的存储库:

public interface MyMongoRepo extends MongoRepository<MyDoc, String> {
    default MyDoc findByIdentifier (MyIdentifier identifier) {
        return findOne(identifier.toString());
    }
}

现在,当我调用 MyMongoRepo#findAll 时,我得到

org.springframework.core.convert.ConverterNotFoundException: 
No converter found capable of converting from type [java.lang.String] 
to type [com.xmpl.NestedDoc]

预期产出:

当我调用 MyMongoRepo#findByIdentifier 时(就像在 RestController 中一样),我得到如下信息:

{
    id: 123,
    nested: {
        identifier: "abc",
        stp: "SOME_CONSTANT",
    }
}

MyMongoRepo#findAll应该return一个包含所有已知MyDocs的数组。

除了问题之外,首先知道为什么需要转换器会很有趣。在需要转换字符串的情况下发生了什么?

您的数据库中有 mongo 个文档,如下所示

{
    id: 1,
    nested: "somevalue"
}

和 spring 无法将 String 转换为 NestedDoc 对象。

Fix/Remove 文件和你应该没问题。