I am playing around with REST service. I have created with swagger editor an API definition and then I have generated the code for a Springboot server. After some changes in Eclipse, all works fine, but I have one "problem", I am not able to override the toString method to display the result as I want. (there is an object called Genre with genreId and definition, I don't want to display the genreId)
This is the method implementation in Controller:
public ResponseEntity<List<Movie>> findMovies(
@ApiParam(value = "Tags used to filter the result") @RequestParam(value =
"tags", required = false) List<String> tags,
@ApiParam(value = "maximum number of results to return") @RequestParam(value =
"limit", required = false) Integer limit) {
List<Movie> movies = movieRepository.findAll();
return new ResponseEntity<List<Movie>>(movies, HttpStatus.OK);
}
This is the toString method in Model class:
@Override
public String toString(){
return "Movie {"+"movieId='" + movieId + '\'' + ", title='" + title + '\'' +",
description='"+ description + '\'' +", rating='"+rating+ '\''
+"price='"+price+ '\'' +"genres='"+genres+ '}';
}
Genres is a list of object Genre that have genreId and definition. I have also this in Genre class:
@Id
@JsonProperty("genre_id")
@JsonIgnore
private String genreId;
private String definition;
this is the toString method:
@Override
public String toString() {
return "Genre {"+"definition='" + definition + '}';
}
I don't want to have genreId in my results, but I have it. Debugging the code I saw that in controller: return new ResponseEntity>(movies, HttpStatus.OK); the movie object does't have the genreId, so for some reasons ResponseEntity print it. I have inserte a brekpoint and the toString method inside Genre is not called
this is an example of result:
{
"genres": [
{
"genreId": "1",
"definition": "Fantasy"
}
],
"movie_id": "2",
"title": "The Hobbit",
"description": "A fantasy journey",
"rating": 6,
"price": 30
}
Some hint?