簡體   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