Log responseEntity with specific fields

Viewed 46

I have an API where the response type is ResponseEntity<Book> and I would want to log the final json response string but with certain fields (e.g. "name" field only) only rather than all fields in Book, what's the best way to do this ?

public class Book implements Serializable {

 @JsonProperty("id")
 private String id;

 @JsonProperty("name")
 private String name;

 @JsonProperty("isbn")
 private String isbn;

 //rest of implementation
}
1 Answers

I tested a demo covers two cases, like that:

Case 1, in which Book has getter methods. If you want to response name only, it will work by ignore id and isbn using @JsonIgnore.

public class Book implements Serializable {

  @JsonIgnore
  private String id;

  private String name;

  @JsonIgnore
  private String isbn;

  //rest of implementation


  public String getId() {
    return id;
  }

  public String getName() {
    return name;
  }

  public String getIsbn() {
    return isbn;
  }
}

Case 2, in which Book doesn't has any getter method. If you want to response name only, it will work by using @JsonProperty on name only.

public class Book implements Serializable {

  private String id;

  @JsonProperty
  private String name;

  private String isbn;

  //rest of implementation

}
Related