2016-09-27 61 views
0

在我的應用程序的Swagger UI中,我有POST和PUT方法,它們都將POJO作爲REST調用中的主體參數。當我查看UI並單擊模型模式以自動填充參數框(爲了節省輸入整個JSON的時間),它不包含參數的名稱,因此請求失敗。例如:Swagger模型模式不包含變量名稱的正文參數

模型模式:

{ 
    "first": "string", 
    "last": "string", 
    "address": "string", 
    "email": "string", 
    . 
    . 
    . 
} 

然而,爲了使請求我需要包括參數名entry像這樣:

{ "entry": { 
    "first": "string", 
    "last": "string", 
    "address": "string", 
    "email": "string", 
    . 
    . 
    . 
}} 

雖然不是太不方便做到這一點我自己在發出請求之前,它已經爲其他開發人員使用Swagger UI爲我的應用程序帶來了問題,而且沒有意識到他們需要添加entry。有沒有辦法修改模型模式?

回答

1

該參數的name未用作body參數中的屬性。爲了描述你的模型,你可以定義一個entry屬性與object類型如下:

definitions: 
    Entry: 
    type: object 
    properties: 
     entry: 
     type: object 
     properties: 
      first: 
      type: string 
      last: 
      type: string 
      address: 
      type: string 
      email: 
      type: string 

,你可以像如下方式使用它作爲體架構:

post: 
    produces: 
    - application/json 
    parameters: 
    - name: body 
     in: body 
     schema: 
     $ref: '#/definitions/Entry' 
+0

我在哪裏可以把這個代碼? – SVN600

+0

把它放在'definitions'下,並用它作爲主體模式。 – Wilson

+0

是否有註釋會自動創建正確的層次結構? – SVN600