1

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?

1 Answer 1

1

Your output is generated by JSON serializer, it does not use toString() method.

It uses field annotations to determine which fields should be serialized (or ignored) to json-format. Mark genreId in your Genre class with @JsonIgnore annotation to exclude this field from JSON output.

Update In some cases (based on JSON serializer configuration/bugs/etc, it is not enough to mark the field with @JsonIgonre, you should also mark the getter of this field too. As it is shown below.

Example:

class Genre {

   @JsonIgnore
   private long genreId;

   @JsonIgnore
   public long getGenreId() { 
      return genreId;
   }
   // the rest of your Genre class ... 
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is wath I have in my code: -@Id -@JsonProperty("genre_id") -@JsonIgnore private String genreId; private String definition;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.