Swagger springfox hide model property on POST

前端 未结 2 2061
悲哀的现实
悲哀的现实 2021-02-05 22:16

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.

Pr

2条回答
  •  醉酒成梦
    2021-02-05 22:51

    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 persons,
                                         HttpServletResponse response) {
    }
    

    Works like charm :)

提交回复
热议问题