I am using spring jpa and lombok to define the java bean Topic. Each topic will have many comments. My onetomany configuration is
@Entity
@Table(name = "TOPIC")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Topic implements Serializable {
@OneToMany(
fetch = FetchType.LAZY,
cascade = CascadeType.REMOVE,
mappedBy = "topic"
)
private Set<Comment> comments= new HashSet<>();
The restful api I created is like this. The serialization seems to be the issue, and it always fetch the comments. As mentioned by Chris, I have added @JsonIgnore
, and it seems solving the issue. But what if I want to load the comments, @JsonIgnore
won't return the comments in the serialization.
@GetMapping("/topics")
public List<Topic> getAllTopics(@SortDefault(sort = "topicName", direction = DESC) Sort sort) {
return topicService.getAllTopics(sort);
}