user1491636
user1491636

Reputation: 2436

Swagger - customize example request body

I have a swaggerized Spring MVC endpoint for a POST operation. The operation takes a path param and a (json) request entity. When the swagger UI is generated, it automatically creates an example value of the request body data type. This example is based on a java entity in the application. The entity is used for some other operations, however for the POST operation, I do not want some of the entity fields exposed in the example. Is it possible to modify this example without modifying the existing java model? For example, is it possible to exclude in the below example lastUpdate.

enter image description here

Upvotes: 4

Views: 2943

Answers (1)

Grammin
Grammin

Reputation: 12205

Yes it is possible to ignore lastUpdate in the example value using jackson annotations JsonIgnore.

import com.fasterxml.jackson.annotation.JsonIgnore;    

public class Phone {
  private String phoneNumber;
  private String lastUpdate;
  private int status;

  public String getPhoneNumber() {
    return phoneNumber;
  }
  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }

  @JsonIgnore
  public String getLastUpdate() {
    return lastUpdate;
  }
  public void setLastUpdate() {
    this.lastUpdate = lastUpdate;
  }

  public int getStatus() {
    return lastUpdate;
  }
  public void setStatus() {
    this.status = status;
  }
}

Upvotes: 2

Related Questions