Swagger springfox hide model property on POST

2019-03-15 16:41发布

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

Problem being I would like to see this in the GET requests through Swagger. But not POST requests, since id is auto-assigned, I would like to hide it just for the 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 on GET, I should see:

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

And on POST, I should see just:

{
   "name"
}

Tried adding: @ApiModelProperty(readonly=true). But that didn't help.

2条回答
ら.Afraid
2楼-- · 2019-03-15 16:54

Unfortunately having different request and response models is not supported currently in springfox. The current thought is that we might support this feature using @JsonView in the future.

查看更多
一夜七次
3楼-- · 2019-03-15 16:57

I have solved this with simply extending the Object that I want to hide a property of when using as request parameter.

Example:

I have the object Person.java:

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 have simply 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 simply like:

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

Works like charm :)

查看更多
登录 后发表回答