无法写入内容:找不到类org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor的序列化程序
简单场景:无法写入内容:找不到类org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor的序列化程序,spring,mongodb,jackson,spring-data,spring-data-mongodb,Spring,Mongodb,Jackson,Spring Data,Spring Data Mongodb,简单场景: @Document(collection = "AUTHOR") public class Author implements Serializable { @Id private String id; @Field("name") private String name; public String getId() {return id;} public void setId(String id) {this.id = id;}
@Document(collection = "AUTHOR")
public class Author implements Serializable {
@Id
private String id;
@Field("name")
private String name;
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
@Document(collection = "BOOK")
public class Book implements Serializable {
@Id
private String id;
@Field("name")
private String name;
@DBRef(lazy = true)
@Field("author")
private Author author;
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public Author getAuthor() {return author;}
public void setAuthor(Author author) {this.author = author;}
}
简单测试:
@Inject
private BookRepository bookRepository;
@Inject
private AuthorRepository authorRepository;
private MockMvc restBookMockMvc;
private Book book;
private Author author;
@PostConstruct
public void setup() {
MockitoAnnotations.initMocks(this);
BookResource bookResource = new BookResource();
ReflectionTestUtils.setField(bookResource, "bookRepository", bookRepository);
this.restBookMockMvc = MockMvcBuilders.standaloneSetup(bookResource).build();
}
@Before
public void initTest() {
authorRepository.deleteAll();
author = new Author();
author.setName("Pedro");
authorRepository.save(author);
bookRepository.deleteAll();
book = new Book();
book.setName(DEFAULT_NAME);
book.setAuthor(author);
}
@Test
public void getBook() throws Exception {
// Initialize the database
bookRepository.save(book);
// Get the book
restBookMockMvc.perform(get("/api/books/{id}", book.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(book.getId()))
.andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.author.name").value(author.getName().toString()));
}
返回状态500和复杂错误:
com.fasterxml.jackson.databind.JsonMappingException:无序列化程序
为班级找到
org.springframework.data.mongodb.core.convert.DefaultDbRefResolver$LazyLoadingInterceptor
并且没有发现创建BeanSerializer的属性(以避免
异常,禁用SerializationFeature。失败(在\u空的\u bean上)
(通过参考链:
com.mycompany.myapp.domain.Book[“author”]->com.mycompany.myapp.domain.author$$enhancerbyglib$$e7a6d2fe[“回调”])
如果将Book类中的Author设置为eager(lazy==false),则它可以工作。我能做什么?这是MongoDB或Jackson本身的Spring数据中的问题吗
我发现了,但似乎没有人在解决这个问题。您有解决方案吗?另一个成本较低的解决方案是使用DTO。不过,我还是希望这个问题也能得到解决。另外,你可能想看看这个