2015-05-01 18 views
8

Bir model özelliğini POST'ta Swagger'da nasıl gizleyeceğinizi öğrenmek isteriz. Ben Swagger-springmvc (0.9.3) ve Springfox (swagger spec 2.0 destekler) hiçbir boşuna denedim.Swagger springfox gizlemek model özelliği POST'ta

Bu sorunu GET isteklerinde Swagger aracılığıyla görmek istiyorum. Ancak POST istekleri değil, kimlik otomatik olarak atandığından, yalnızca POST isteğinde bulunmak istiyorum.

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

Yani GET, ben görmelisiniz:
{ 
    "id": 0, 
    "name" : "abc" 
} 

Ve POST üzerinde

, sadece görmelisiniz:

{ 
    "name" 
} 

ekleyerek çalıştı: @ApiModelProperty (salt okunur = true). Ama bu yardımcı olmadı.

cevap

1

Yaygın olarak farklı istek ve yanıt modellerine sahip olmak springfox'ta desteklenmiyor. Mevcut düşünce, gelecekte bu özelliği @JsonView kullanarak destekleyebileceğimiz.

+0

Bu konuda herhangi bir güncelleme var mı? – SimonH

+0

@Dilip Krishnan, bunun uygulanacağını da çok seviriz. Bu, zaten uygulanıyorsa (https://github.com/springfox/springfox/pull/2056) henüz eklenmemişse, ancak bu işlem gerçekleşmeyecekse –

+0

@ MariánZekeŠedaj Yakında –

2

Bunu, istek parametresini kullanırken bir özelliği gizlemek istediğim Nesneyi genişletmekle çözdüm.

Örnek:

Ben nesne Person.java: PersonRequest.java:

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

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

RequestMapping basitçe görünüyor gibi:

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

Sadece Başka bir sınıf oluşturduk

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

Çekicilik gibi çalışır