繁体   English   中英

如何仅针对特定响应隐藏 Swagger-ui 中的嵌套属性?

[英]How to hide nested attributes in Swagger-ui only for certain response?

我有一个 class,其属性如下。

Class AnimalResponse {

@JsonProperty("animals")
private List<Animal> animals = new ArrayList<>();

@JsonProperty("someOtherAttributes")
private List<someOtherAttribute> someOtherAttributes= new ArrayList<>();

}

Animal.class如下所示:

Class Animal {

@JsonProperty("id")
private int id;

@JsonProperty("name")
private String name;

@JsonProperty("height")
private float height;
}

我不想序列化Animal.class中的属性height

所以,我执行以下两个步骤:

  1. 我创建了一个 mixIn 忽略高度

    公共接口 HeightIgnoreMixIn {

     @JsonIgnore String getHeight();

    }

  2. 在序列化期间,我添加了这个 mixin。

    new ObjectMapper().addMixIn(Animal.class, HeightIgnoreMixIn.class).writer().writeValueAsString(object);

这非常有效。

**Note: Animal.class is used by other classes.** 

我也不想在 Swagger UI 中显示此height 我该怎么做呢?

我无法将@ApiModelProperty(hidden=true)添加到height ,因为Animal.class被其他类使用,他们希望它在 Swagger UI 中可见。 所以,我只想从 Swagger UI 的AnimalResponse中删除它。 我该怎么做呢?

创建两个不同的DTO类。 一个有height场,第二个没有height场。

这是因为使用一个 class swagger 将使用一个链接两次 - 您可以通过展开 swagger UI 页面底部的“模型”块看到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM