Spring MongoRepository#findall: ConverterNotFoundException

520 views Asked by At

I have two simple documents MyDoc and NestedDoc

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

    // ...

}

There is a straight forward repository:

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

Now, when I call MyMongoRepo#findAll I get

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

Expected Outpout:

When I call MyMongoRepo#findByIdentifier (like in a RestController) I get something like:

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

and MyMongoRepo#findAll should return an array that contains all known MyDocs.

In addition to the problem, it would be interesting to know why a converter is needed in the first place. What happens under the hood that requires a String to be converted?

1

There are 1 answers

1
s7vr On BEST ANSWER

You have mongo document in your database which look like below

{
    id: 1,
    nested: "somevalue"
}

and spring fails to convert String into NestedDoc object.

Fix/Remove the document and you should be fine.