2015-05-01 11 views
8

Vorrei sapere come nascondere una proprietà del modello in Swagger su POST. Ho provato sia Swagger-springmvc (0.9.3) che Springfox (supporta swagger spec 2.0) senza alcun risultato.Proprietà del modello swagger springfox hide su POST

Il problema è che mi piacerebbe vederlo nelle richieste GET tramite Swagger. Ma non le richieste POST, dato che l'ID è auto-assegnato, vorrei nasconderlo solo per la richiesta 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; 
    } 
} 

Così via GET, mi dovrebbe vedere:

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

E sul post, mi dovrebbe vedere solo:

{ 
    "name" 
} 

provato ad aggiungere: @ApiModelProperty (sola lettura = true). Ma quello non ha aiutato.

risposta

1

Purtroppo diversi modelli di richiesta e risposta non sono attualmente supportati in springfox. L'idea attuale è che potremmo supportare questa funzione usando @JsonView in futuro.

+0

qualsiasi aggiornamento su questo? – SimonH

+0

@Dilip Krishnan ci piacerebbe anche sapere se questo sarà implementato –

+0

@ MariánZekeŠedaj questo è già [stato implementato] (https://github.com/springfox/springfox/pull/2056) non è ancora unito, ma sarà sii presto –

2

Ho risolto questo con semplicemente estendendo l'oggetto che voglio nascondere una proprietà di quando si utilizza come parametro di richiesta.

Esempio:

ho l'oggetto 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; 
    } 
} 

ho semplicemente creato un'altra classe: PersonRequest.java:

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

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

RequestMapping appare semplicemente come:

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

Funziona come fascino :)

Problemi correlati