我使用SpringBootRestWeb服务和Angular5作为前端,我有一个hibernate的模型类,如下所示:
@Entity
public class Title {
private Integer id;
private String name;
private Date releaseDate;
private Time runtime;
private String storyline;
private String picture;
private String rated;
private String type;
private Double rating;
private Integer numberOfVotes;
private Timestamp inserted;
private Set<Genre> genres = new HashSet<>();
private List<TitleCelebrity> titleCelebrities;
private List<TitleMedia> titleMedia;
// Basic getters and setter
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "title_genre", joinColumns = { @JoinColumn(name = "title_id") }, inverseJoinColumns = { @JoinColumn(name = "genre_id") })
public Set<Genre> getGenres() {
return genres;
}
public void setGenres(Set<Genre> genres) {
this.genres = genres;
}
@OneToMany(mappedBy = "title", cascade = CascadeType.ALL)
public List<TitleCelebrity> getTitleCelebrities() {
return titleCelebrities;
}
public void setTitleCelebrities(List<TitleCelebrity> titleCelebrities) {
this.titleCelebrities = titleCelebrities;
}
@OneToMany(mappedBy = "title", cascade = CascadeType.ALL)
public List<TitleMedia> getTitleMedia() {
return titleMedia;
}
public void setTitleMedia(List<TitleMedia> titleMedia) {
this.titleMedia = titleMedia;
}
}
这是我的休息控制器
@RestController
@RequestMapping("titles")
@CrossOrigin(origins = {"http://localhost:4200"})
public class TitleController {
private TitleService titleService;
@Autowired
public void setTitleService(TitleService titleService) {
this.titleService = titleService;
}
// Api to get all the movies ordered by release date
@GetMapping("movies")
public List<Title> getAllMoviesOrderByReleaseDateDesc() {
return this.titleService.findByTypeOrderByReleaseDateDesc("movie");
}
@GetMapping("movies/{id}")
public Title findById(@PathVariable Integer id) {
return this.titleService.findById(id);
}
}
我想要的是当我向第一个方法'/movies'发出请求时,我不想要titleMedia的集合,但是如果我向第二个方法'/movies/id'发出请求,我想要titleMedia的集合。
当然是注释
@JsonIgnore
无论请求是什么,都将忽略集合。