Swagger springfox hides model property on POST

I would like to know how to hide the model property in Swagger on POST. I tried both Swagger-springmvc (0.9.3) and Springfox (supports swagger spec 2.0) to no avail.

The problem is that I would like to see this in GET requests via Swagger. But not POST requests, since id is automatically assigned, I would like to hide it only for POST request.

public class RestModel { private int id; @JsonProperty private String name; @JsonProperty public int getId() { return 0; } @JsonIgnore public void setId(int customerId) { this.customerId = customerId; } public int getName() { return "abc"; } public void setName(String name) { this.name = name; } } 

So GET, I should see:

 { "id": 0, "name" : "abc" } 

And on POST, I should only see:

 { "name" } 

Tried to add: @ApiModelProperty (readonly = true). But it did not help.

+8
swagger swagger-ui
source share
2 answers

I solved this by simply extending Object so that I wanted to hide the property when used as a request parameter.

Example:

I have a Person.java object:

 import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonView; import org.joda.time.DateTime; import org.joda.time.Years; import java.util.Date; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import io.swagger.annotations.ApiModelProperty; /** * Simple Person pojo */ @Entity public class Person { @GeneratedValue(strategy = GenerationType.AUTO) @Id private Long dbId; private String name; private Long id; @JsonFormat(pattern="yyyy-MM-dd") private Date birthDate; private String gender; public Person() { } public Person(long dbId) { this.dbId = dbId; } public Person(Long id, String name, Date birthDate, String gender) { this.id = id; this.name = name; this.birthDate = birthDate; this.gender = gender; } public Long getDbId() { return dbId; } public String getName() { return name; } public Date getBirthDate() { return birthDate; } public String getGender() { return gender; } public Integer getAge() { return Years.yearsBetween(new DateTime(birthDate), new DateTime()).getYears(); } public void setDbId(Long dbId) { this.dbId = dbId; } public void setName(String name) { this.name = name; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public void setGender(String gender) { this.gender = gender; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } } 

I just created another class: PersonRequest.java:

 import com.fasterxml.jackson.annotation.JsonIgnore; public class PersonRequest extends Person { @Override @JsonIgnore public void setDbId(Long dbId) { super.setDbId(dbId); } } 

RequestMapping looks simple:

 @RequestMapping(value = "/KVFirstCare/application", method = RequestMethod.POST) public ApplicationResult application(@RequestBody List<PersonRequest> persons, HttpServletResponse response) { } 

It works like a charm :)

+2
source share

Unfortunately, Springfox does not currently support different request and response models. The current thought is that we can support this feature with @JsonView in the future.

+1
source share

All Articles