覆盖 EntityResponse toString 方法
Override EntityResponse toString method
我正在研究 REST 服务。
我用 swagger 编辑器创建了一个 API 定义,然后我为 Springboot 服务器生成了代码。
在 Eclipse 中进行一些更改后,一切正常,但我有一个 "problem",我无法重写 toString 方法来显示我想要的结果。
(有个叫Genre的对象有genreId和definition,我不想显示genreId)
这是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);
}
这是Model中的toString方法class:
@Override
public String toString(){
return "Movie {"+"movieId='" + movieId + '\'' + ", title='" + title + '\'' +",
description='"+ description + '\'' +", rating='"+rating+ '\''
+"price='"+price+ '\'' +"genres='"+genres+ '}';
}
Genres 是具有 genreId 和定义的对象 Genre 的列表。
我在流派 class:
中也有这个
@Id
@JsonProperty("genre_id")
@JsonIgnore
private String genreId;
private String definition;
这是 toString 方法:
@Override
public String toString() {
return "Genre {"+"definition='" + definition + '}';
}
我不想在结果中包含 genreId,但我有。
调试我在控制器中看到的代码:
return 新的 ResponseEntity>(电影,HttpStatus.OK);
电影对象没有 genreId,所以对于某些
ResponseEntity 打印它的原因。我插入了一个断点,但未调用 Genre 中的 toString 方法
这是结果示例:
{
"genres": [
{
"genreId": "1",
"definition": "Fantasy"
}
],
"movie_id": "2",
"title": "The Hobbit",
"description": "A fantasy journey",
"rating": 6,
"price": 30
}
有什么提示吗?
您的输出是由 JSON 序列化程序生成的,它不使用 toString()
方法。
它使用字段注释来确定应将哪些字段序列化(或忽略)为 json 格式。在 Genre
class 中用 @JsonIgnore
注释标记 genreId
以从 JSON 输出中排除此字段。
更新
在某些情况下(基于JSON序列化器configuration/bugs/etc,仅仅用@JsonIgonre
标记字段是不够的,你还应该标记这个字段的getter。因为它如下所示。
示例:
class Genre {
@JsonIgnore
private long genreId;
@JsonIgnore
public long getGenreId() {
return genreId;
}
// the rest of your Genre class ...
}
我正在研究 REST 服务。 我用 swagger 编辑器创建了一个 API 定义,然后我为 Springboot 服务器生成了代码。 在 Eclipse 中进行一些更改后,一切正常,但我有一个 "problem",我无法重写 toString 方法来显示我想要的结果。 (有个叫Genre的对象有genreId和definition,我不想显示genreId)
这是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);
}
这是Model中的toString方法class:
@Override
public String toString(){
return "Movie {"+"movieId='" + movieId + '\'' + ", title='" + title + '\'' +",
description='"+ description + '\'' +", rating='"+rating+ '\''
+"price='"+price+ '\'' +"genres='"+genres+ '}';
}
Genres 是具有 genreId 和定义的对象 Genre 的列表。 我在流派 class:
中也有这个@Id
@JsonProperty("genre_id")
@JsonIgnore
private String genreId;
private String definition;
这是 toString 方法:
@Override
public String toString() {
return "Genre {"+"definition='" + definition + '}';
}
我不想在结果中包含 genreId,但我有。 调试我在控制器中看到的代码: return 新的 ResponseEntity>(电影,HttpStatus.OK); 电影对象没有 genreId,所以对于某些 ResponseEntity 打印它的原因。我插入了一个断点,但未调用 Genre 中的 toString 方法
这是结果示例:
{
"genres": [
{
"genreId": "1",
"definition": "Fantasy"
}
],
"movie_id": "2",
"title": "The Hobbit",
"description": "A fantasy journey",
"rating": 6,
"price": 30
}
有什么提示吗?
您的输出是由 JSON 序列化程序生成的,它不使用 toString()
方法。
它使用字段注释来确定应将哪些字段序列化(或忽略)为 json 格式。在 Genre
class 中用 @JsonIgnore
注释标记 genreId
以从 JSON 输出中排除此字段。
更新
在某些情况下(基于JSON序列化器configuration/bugs/etc,仅仅用@JsonIgonre
标记字段是不够的,你还应该标记这个字段的getter。因为它如下所示。
示例:
class Genre {
@JsonIgnore
private long genreId;
@JsonIgnore
public long getGenreId() {
return genreId;
}
// the rest of your Genre class ...
}