简体   繁体   English

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

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

I have a class with attributes as below.我有一个 class,其属性如下。

Class AnimalResponse {

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

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

}

Animal.class looks like below: Animal.class如下所示:

Class Animal {

@JsonProperty("id")
private int id;

@JsonProperty("name")
private String name;

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

I do not want to serialize the attribute height inside Animal.class .我不想序列化Animal.class中的属性height

So, I do the following 2 steps:所以,我执行以下两个步骤:

  1. I create a mixIn ignoring height我创建了一个 mixIn 忽略高度

    public interface HeightIgnoreMixIn {公共接口 HeightIgnoreMixIn {

     @JsonIgnore String getHeight();

    } }

  2. During serialization, I add this mixin.在序列化期间,我添加了这个 mixin。

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

This works perfectly.这非常有效。

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

I do not want to show this height in Swagger UI as well.我也不想在 Swagger UI 中显示此height How do I do this?我该怎么做呢?

I can't add @ApiModelProperty(hidden=true) to height because Animal.class is used by other classes and they want it visible in the Swagger UI.我无法将@ApiModelProperty(hidden=true)添加到height ,因为Animal.class被其他类使用,他们希望它在 Swagger UI 中可见。 So, I only want to remove it from the AnimalResponse in Swagger UI.所以,我只想从 Swagger UI 的AnimalResponse中删除它。 How do I do this?我该怎么做呢?

Create two different DTO classes.创建两个不同的DTO类。 One with a height field, the second without a height field.一个有height场,第二个没有height场。

This is because with one class swagger uses will use one link twice - you can see this by expanding the "Models" block at the bottom of the swagger UI page.这是因为使用一个 class swagger 将使用一个链接两次 - 您可以通过展开 swagger UI 页面底部的“模型”块看到这一点。

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

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