J'ai deux documents simples MyDoc
et NestedDoc
Spring MongoRepository # findall: ConverterNotFoundException
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;
}
// ...
}
Il y a un dépôt simple:
public interface MyMongoRepo extends MongoRepository<MyDoc, String> {
default MyDoc findByIdentifier (MyIdentifier identifier) {
return findOne(identifier.toString());
}
}
Maintenant, quand je l'appelle MyMongoRepo#findAll
je reçois
org.springframework.core.convert.ConverterNotFoundException:
No converter found capable of converting from type [java.lang.String]
to type [com.xmpl.NestedDoc]
attendu outpout:
Quand j'appelle MyMongoRepo#findByIdentifier
(comme dans un RestController) je reçois quelque chose comme:
{
id: 123,
nested: {
identifier: "abc",
stp: "SOME_CONSTANT",
}
}
et MyMongoRepo#findAll
devrait retourner un tableau qui contient tous les MyDocs connus.
En plus du problème, il serait intéressant de savoir pourquoi un convertisseur est nécessaire en premier lieu. Qu'est-ce qui se passe sous le capot qui nécessite une chaîne à convertir?
Pourquoi je n'ai pas inventé ça ?! C'est tellement évident! Haha, merci l'homme. Dans mon application, j'ai plusieurs référentiels, et au démarrage, je supprime généralement toutes les collections. J'ai oublié cette ligne pour ce repo particulier. J'avais donc des documents obsolètes de versions antérieures «imbriquées» quand elles étaient simples ... Dieu, je me déteste tout de suite! –