2017-09-13 38 views
0

我有兩個簡單的文檔MyDocNestedDoc春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; 
    } 

    // ... 

} 

有一個簡單的存儲庫:

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] 

預計Outpout:

當我(在RestController等)調用MyMongoRepo#findByIdentifier我得到的是這樣的:

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

MyMongoRepo#findAll應該返回一個包含所有已知MyDoc的數組。

除了這個問題之外,知道爲什麼首先需要轉換器是很有趣的。在需要轉換字符串的引擎蓋下發生了什麼?

回答

1

你在你的數據庫文件蒙哥看起來像下面

{ 
    id: 1, 
    nested: "somevalue" 
} 

,春季未能String轉換爲NestedDoc對象。

修復/刪除文件,你應該沒問題。

+0

爲什麼我沒有拿出那個?很明顯!哈哈,謝謝你。在我的應用程序中,我有多個存儲庫,並且在啓動時我通常會刪除所有集合。忘記那個特定的回購行。所以我過時的早期嵌套版本的文件,當他們是簡單的字符串...上帝,我現在恨我自己! –

相關問題