Swagger springfox 在 POST 上隐藏模型属性

2024-02-18

想知道如何在 POST 时隐藏 Swagger 中的模型属性。我尝试过Swagger-springmvc(0.9.3)和Springfox(支持swagger规范2.0),但均无济于事。

问题是我想在通过 Swagger 的 GET 请求中看到这一点。但不是 POST 请求,因为 id 是自动分配的,我想仅为 POST 请求隐藏它。

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;
   }
}

所以在 GET 上,我应该看到:

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

在 POST 上,我应该看到:

{
   "name"
}

尝试添加:@ApiModelProperty(readonly=true)。但这没有帮助。


我通过简单地扩展我想在用作请求参数时隐藏其属性的对象来解决这个问题。

Example:

我有对象 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;
    }
}

我只是创建了另一个类:PersonRequest.java:

import com.fasterxml.jackson.annotation.JsonIgnore;
public class PersonRequest extends Person {

    @Override
    @JsonIgnore
    public void setDbId(Long dbId) {
        super.setDbId(dbId);
    }
}

RequestMapping 看起来很简单:

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

就像魅力一样有效:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swagger springfox 在 POST 上隐藏模型属性 的相关文章

随机推荐