Recursive JSON view of an entity with one-to-many

2020-02-26 09:35发布

I'm using SpringBoot and JPA to build a REST interface.

Now, I have a strange JSON returned for the list of products fetched from the database. Let's say that I have:

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "categoryId", nullable = false, updatable = false)
    private Category category;

    ...
}

@Entity
public class Category implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy = "category", cascade = CascadeType.DETACH)
    @OrderBy("name ASC")
    private List<Product> products = Collections.emptyList();

    ...
}

The JPA repository for the Product is defined as:

public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findAll();
}

In my controller I have:

@Autowired
private ProductRepository productRepo;

@RequestMapping("/all-products", method = RequestMethod.GET)
public Map<String,Object> home() {
    Map<String,Object> model = new HashMap<String,Object>();
    model.put("products", productRepo.findAll());
    return model;
}

What is driving my crazy, is that if I try to call this service as follows:

$ curl localhost:8080/all-products

I get a recursive output due to the relationship between tables product and category, e.g.:

{"products":[{"id":1,"name":"Product1","category":
{"id":1,"name":"Cat1","products":[{"id":6,"name":"Product6","category":
{"id":1,"name":"Cat1","products":[{"id":6,"name":"Product6","category":
{"id":1,...

What am I doing wrong?

3条回答
叼着烟拽天下
2楼-- · 2020-02-26 09:53

You're not doing anything wrong (at least at the code level it's rather conceptual) - json serializer just goes like this:

  1. Product - serialize it, but wait - there is a category field, so serializer must serialize the category field
  2. Category - serialize it, but wait - there is a products field, so serializer must serialize each of the product in the list
  3. Product - because your collection contains the product & product contains category it goes in a endless loop untill a timeout.

You must use a view or just skip it.

  1. Use @JsonView

  2. Use a view as a POJO Return new ProductView that has all fields of product and a reference (category) to new CategoryView (you can end at this point) that has collection of (products) new ProductViewWithoutReferences, and so on

  3. Use @JsonIgnore on a collection of products

And as a side note - if it's a @RestController and you're invoking "all-products" then it's a bit unusual to return something else than a list. Wrapping the response in a map is redundant. Many rest clients expect a list when they invoke list() method.

查看更多
疯言疯语
3楼-- · 2020-02-26 09:55

Adding @JsonIgnore worked for me

@OneToMany(mappedBy = "policy")
@JsonIgnore
private List<Payment> payments;

@JeanValjean your are the best

查看更多
Ridiculous、
4楼-- · 2020-02-26 10:03

I know it's a bit late, but adding it here in case anybody faces the same problem. Here is another relevant answer I could find which discuss about similar topic

https://stackoverflow.com/a/3359884/6785908

quoting it here

Jackson 1.6 has annotation-based support for handling such parent/child linkage, see http://wiki.fasterxml.com/JacksonFeatureBiDirReferences.

You can of course already exclude serialization of parent link already using most JSON processing packages (jackson, gson and flex-json at least support it), but the real trick is in how to deserialize it back (re-create parent link), not just handle serialization side. Although sounds like for now just exclusion might work for you.

EDIT (April 2012): Jackson 2.0 now supports true identity references, so you can solve it this way also.

查看更多
登录 后发表回答