![](/img/trans.png)
[英]java.lang.IllegalArgumentException thrown when trying to an set instance by reflection
[英]When MongoDB DBRef lazyness is set to true, java.lang.IllegalArgumentException is throw
我有以下課程:
@Document(collection = "T_FOO")
public class Foo implements Serializable {
@Field
private String name;
@Field
private String observations;
@DBRef
@Field
private Foo[] parents;
}
在此測試中成功的是:
@Test
public void testFooWithParents() throws Exception {
//mock User
User user = new User(); user.setLogin("admin");
when(userService.getUserWithAuthorities()).thenReturn(user);
// Create Father
restFooMockMvc.perform(post("/app/rest/foos")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJson(fooFather)))
.andExpect(status().isOk());
// Create Mother
restFooMockMvc.perform(post("/app/rest/foos")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJson(fooMother)))
.andExpect(status().isOk());
foo.setParents(new Foo[]{fooFather, fooMother});
// Create Foo
restFooMockMvc.perform(post("/app/rest/foos")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJson(foo)))
.andExpect(status().isOk());
// Read Foo
MvcResult result = restFooMockMvc.perform(get("/app/rest/foos/{id}", DEFAULT_ID))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(DEFAULT_ID))
.andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.observations").value(DEFAULT_OBSERVATIONS))
.andExpect(jsonPath("$.parents[0].id").value(FATHER_ID))
.andExpect(jsonPath("$.parents[1].id").value(MOTHER_ID))
.andReturn();
String content = result.getResponse().getContentAsString();
}
當我將parents數組設置為lazy = true
,我得到了這個異常:
org.springframework.web.util.NestedServletException:
Request processing failed; nested exception is java.lang.IllegalArgumentException:
Cannot subclass final class class [Lorg.domain.Foo;
這是在我請求Foo兒子(“讀取Foo”)時拋出的。 Spring無法再次重建對象,這是怎么回事?
最好,佩德羅。
僅當屬性類型為非最終類或接口時,才支持在Spring Data MongoDB中延遲加載Db-Refs,因為我們需要能夠為其創建JDK或CGLib代理。 不支持數組。 我創建了https://jira.spring.io/browse/DATAMONGO-1157進行跟蹤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.