Swagger springfox在POST上隐藏模型属性 [英] Swagger springfox hide model property on POST

查看:22
本文介绍了Swagger springfox在POST上隐藏模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如何在 POST 时在 Swagger 中隐藏模型属性.我试过 Swagger-springmvc (0.9.3) 和 Springfox (supports swagger spec 2.0) 都无济于事.

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.

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

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

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

So on GET, I should see:

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

在 POST 上,我应该只看到:

And on POST, I should see just:

{
   "name"
}

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

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

推荐答案

我已经解决了这个问题,只需扩展我想在用作请求参数时隐藏其属性的对象.

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

例子:

我有对象 Person.java:

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

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

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 看起来很简单:

RequestMapping looks simply like:

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

像魅力一样工作:)

这篇关于Swagger springfox在POST上隐藏模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆