1

I've recently added a @ManyToOne & @OneToMany association on two of the @Entitys in my Spring MVC Project.

@Entity
public class Book {

    @ManyToOne
    @JoinColumn(name = "book_id")
    private BookCategory category;

}

@Entity
public class BookCategory{

    @OneToMany(mappedBy = "category")
    private List<Book> books;

}

@RequestMapping(value = "getAllBooks", produces = "application/json")
public @ResponseBody List<Book> getAllBooks(){
    return bookRepo.findAll(); // Native Spring JPA method
}

Before including the joins, I could easily populate a List of Books (without their BookCategories) and send them to the client as a JSON response.

However, after including the joins my asynchronous request fails and I get the following error in Chrome

SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>)

On IE:

Unterminated string constant: {description: "...

Also in Chrome I see

Resource interpreted as Document but transferred with MIME type application/json:

The request itself runs fine, returning a 200, and the response looks like normal JSON array of objects. Any ideas why this might be occuring?

Thank you!

Clay Banks
  • 4,483
  • 15
  • 71
  • 143

2 Answers2

1

posible problem could be endless json. each book contains a category, ecach category contains same book, that contains category, that contains the same book and soo on

book: {
  category: {
    book: {
      category: {
        book: {
    .. and so on

so you mapper can convert you object to a json-like string

EDIT: you can fix it using Views.

create BookView class and BookCategoryView class

public class BookCategoryView {

// contains all nessesery fields: id, name ..
// contains getters and setters
// DOESN'T contain book field

}

public class BookView {
  // contains all nessesery fields: id, name ..
  // contains getters and setters

  // here add List<BookCategoryView>
}

if you convert you entity objects to this view object, that does not have endless circle dependencies than you can omit that error

MORE INFO: usually such objects call DTO's(Data Transfer Object). So if you named them BookDTO and BookCategoryDTO everything will be clear

Oleh Kurpiak
  • 1,339
  • 14
  • 34
  • This seems like it could be the issue. Is there a way to prevent this? – Clay Banks Aug 24 '17 at 12:44
  • @KuraiBankusu, in my answer I add the easiest solution – Oleh Kurpiak Aug 24 '17 at 12:50
  • Thanks for the response. it seems that @JsonIgnore resolved this issue as well (see https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue). I'll report back if I find anything missing – Clay Banks Aug 24 '17 at 12:55
0

I fixed this issue by adding @JsonIgnore to my BookCategory Entity.

@JsonIgnore
@OneToMany(mappedBy = "category")
private List<Book> books;

Description:

@JsonIgnore - Marker annotation that indicates that the annotated method or field 
is to be ignored by introspection-based serialization and deserialization functionality. 

As Oleh pointed out this response looked like an endless recursive JSON response. Including the annotation prevents this and returns the appropriate response

Clay Banks
  • 4,483
  • 15
  • 71
  • 143