Failed to write content: serializer not found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver $ LazyLoadingInterceptor

A simple scenario:

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

A simple test:

    @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()));
    }

Returns status 500 and a complex error:

com.fasterxml.jackson.databind.JsonMappingException: no serializer found for class org.springframework.data.mongodb.core.convert.DefaultDbRefResolver $ LazyLoadingInterceptor and no properties found to create a BeanSerializer (to avoid an exception, disable SerializationFONEMFAIL) () through the reference chain: com.mycompany.myapp.domain.Book ["author"] → com.mycompany.myapp.domain.Author $$ EnhancerByCGLIB $$ e7a6d2fe ["callbacks]])

lazy = false, . ? Spring MongoDB ?

JIRA, , .

+4

Source: https://habr.com/ru/post/1589480/


All Articles