0

I'm learnig Spring boot Rest but I can't resolve a problem. Can someone help me? I created the following mapping between the Product and Category entities:

Category Entity:

@Entity
public class Category {
 ...
@JsonIgnore
@OneToMany(mappedBy="category")
private List<Product> products;
//getters setters
}

Product Entity:

@Entity
@Table(name="lancamento")
public class Lancamento {
...
@NotNull
@ManyToOne
@JoinColumn(name="id_pessoa")
private Pessoa pessoa;
//getters setters
}

I wait for this result:

{
 "name":"Category_1",
  "products":[
     {
       "id":"1",
       "name":"Product_1"
     },
     {
      "id":"2",
       "name":"Product_2"
     },
   ]
}

But the real result is:

{
 "name":"Category_1"
}

What is wrong? Is there some settings that I need to make? Thanks.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Anna
  • 11
  • Just remove @JsonIgnore annotation)) – Arkady Aug 17 '18 at 12:29
  • when I remove @JsonIgnore, I received cyclic dependency error: ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON document: Infinite recursion (StackOverflowError) – Anna Aug 17 '18 at 14:05

5 Answers5

2

Try to remove the @JsonIgnore annotation. With this annotation you are telling Spring basically to ignore the field while serializing/deserializing the object, hence it won't be present in the output.

See the Docs

Ph03n1x
  • 794
  • 6
  • 12
  • when I remove @JsonIgnore, I received cyclic dependency error: ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON document: Infinite recursion (StackOverflowError) – Anna Aug 17 '18 at 14:04
0

You have JsonIgnore on products field of Category

Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24
0

Remove @JsonIgnore annotation.

Because of @JsonIgnore annotation this list is getting ignored.

See this https://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonIgnore.html to understand @JsonIgnore.

Alien
  • 15,141
  • 6
  • 37
  • 57
0

You have to romove @JsonIgnore and use DTO-objects for serialization. Anyway this is the proper way to create APIs. You can combine and transform all data from entities in your DTOs as you like.

OR

you can put reverse relation (field) it the Product object, like

@JsonIgnore
@ManyToOne
private Category category;

and mark with @JsonIgnore that field.

It's up to you, but better way is to use DTOs

Arkady
  • 1,178
  • 14
  • 35
  • Thanks :) , I solved the problem using 'code'@JsonManagedReference and @JsonBackReference – Anna Aug 19 '18 at 01:06
0

Thanks for all, I found the solution in Infinite Recursion with Jackson JSON and Hibernate JPA issue

the solution for this case is to use the Jackson annotations: @JsonManagedReference and @JsonBackReference.

Category:

@Entity
public class Category {
...
@JsonManagedReference 
@OneToMany(mappedBy="category")
private List<Product> products;
//getters setters
}

Product

@Entity
public class Product {
...

@JsonBackReference
@NotNull
@ManyToOne
@JoinColumn(name="id_product")
private Product product;
//getters setters
}

This works like I need.

Anna
  • 11