gpt4 book ai didi

Swagger springfox 在 POST 上隐藏模型属性

转载 作者:行者123 更新时间:2023-12-04 02:22:56 62 4
gpt4 key购买 nike

想知道如何在 POST 中隐藏 Swagger 中的模型属性。我已经尝试过 Swagger-springmvc (0.9.3) 和 Springfox(支持 swagger spec 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)。但这没有帮助。

最佳答案

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

例子:

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

像魅力一样工作:)

关于Swagger springfox 在 POST 上隐藏模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29995077/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com