Reputation:
I am using Spring Boot REST Web Services and Angular 5 as a frontend, well I have a model class for hibernating like this :
@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;
}
}
And here's my REST controller
@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);
}
}
What I want is when I make a request to the first method '/movies' i don't want the collection of Telemedia, but if I make a request to the second method '/movies/id' i want the collection of Telemedia.
of course, the annotation @JsonIgnore
will ignore the collection whatever the request is.
Upvotes: 1
Views: 1016
Reputation: 202
It may be better to create two models in this case; one to represent the first response and another to represent the second response.
You could also set the collection to null
in your second request before sending it back.
You cannot accomplish this with @JsonIgnore
alone as you cannot perform conditional logic in annotations.
Upvotes: 1