简体   繁体   中英

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

I have a class with attributes as below.

Class AnimalResponse {

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

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

}

Animal.class looks like below:

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 .

So, I do the following 2 steps:

  1. I create a mixIn ignoring height

    public interface HeightIgnoreMixIn {

     @JsonIgnore String getHeight();

    }

  2. During serialization, I add this mixin.

    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. 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. So, I only want to remove it from the AnimalResponse in Swagger UI. How do I do this?

Create two different DTO classes. One with a height field, the second without a height field.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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