English 中文(简体)
Spring Boot - Spring Data JPA - Rest/Json - How to custom the output to include or not the Collection for @OneToMany?
原标题:

For Spring Boot working with Spring Data for a uni-directional @OneToMany relation, such as Parent and Child, their codes are respectively as follows:

@Entity
@Table(name="parent")
public class Parent implements Serializable {

    ...

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.REMOVE, orphanRemoval=true)
    @JoinColumn(name="parent_id")
    @OrderBy("id")
    private final Set<Child> children = new LinkedHashSet<>();

    ...

}

and

@Entity
@Table(name="child")
public class Child implements Serializable {

    ...

}

And having a custom ParentRepository interface extending CrudRepository<T,ID>, it has defined explicitly a custom method named findByIdWithChildren working with @Query and using JOIN FETCH

Therefore using Spring MVC - through HTML (Thymeleaf) - through @GetMapping is possible:

Group I

  • (A) Find and show only a specific Parent - through the default findById method. (Ok)
  • (B) Find and show a specific Parent with all its Child - through the custom findByIdWithChildren method. (Ok)
  • (C) Find and show only all the Parent - through the default findAll method. (Ok)

Therefore until here all is Ok. The problem is when REST is used, exists now the following behavior as follows.

Group II

  • (A) behaves as (B) Group I (Not desired)
  • (B) behaves as (B) Group I (Ok)
  • (C) Find and show all the Parent but with all their Child (Not desired)

Using @JsonBackReference as follows

    @JsonBackReference
    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.REMOVE, orphanRemoval=true)
    @JoinColumn(name="parent_id")
    @OrderBy("id")
    private final Set<Child> children = new LinkedHashSet<>();

happens as follows:

Group III

  • (A) Find and show only a specific Parent - through the default findById method. (OK)
  • (B) Find and show a specific Parent but without all its Child - through the custom findByIdWithChildren method. (Not desired)
  • (C) Find and show only all the Parent - through the default findAll method. (OK)

Question

  • How to reach the same kind of output for Rest as HTML?

Show only a specific parent (without its children), show a specific parent with its children, show only all the parents (without their children)

问题回答

暂无回答




相关问题
Allow RESTful DELETE method in asp.net mvc?

im currently setting up asp.net to accept DELETE http verb in the application. However, when i send "DELETE /posts/delete/1" i always get a 405 Method not allow error. I tried to take a look at ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

Use HTTPClient or HttpUrlConnection? [closed]

We re implementing a REST client on JRE 1.4. Seems two good options for a client REST framework are HttpClient and HttpUrlConnection. Is there a reason to use HttpClient over the JRE s ...

Why can t I find the truststore for an SSL handshake?

I m using the Spring RESTTemplate on the client side to make calls to a REST endpoint. The client in this case is a Spring app and Tomcat is the servlet container. I m running into issues making a ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Three Step Buyonline The RESTful way

We are re-developing our buyonline functionality and we are doing it the RESTful way. The process is a three step one and the customer is asked to enter data at each step. Let s say the three URL s ...

热门标签