2016-12-06 30 views
15

我有一類,其中有一個特性是List<String>招搖@ApiModelProperty示例值<String>屬性

public class MyClass { 
    .... 
    @ApiModelProperty(position = 2) 
    private List<String> productIdentifiers; 
    .... 
} 

此代碼如下生成的示例值:

{ 
    "customerId": "1001", 
    "productIdentifiers": [ 
    "string" 
    ], 
    "statuses": [ 
    "NEW" 
    ] 
} 

示例這裏顯示的值無效。我的預期示例值應該是這樣的:

{ 
    "customerId": "1001", 
    "productIdentifiers": [ 
    "PRD1", 
    "PRD2", 
    "PRD3" 
    ], 
    "statuses": [ 
    "NEW" 
    ] 
} 

我試圖通過例如屬性如下,但它沒有產生應有的價值:

@ApiModelProperty(position = 2, example = "PRD1, PRD2, PRD3") 
// This generates -> "productIdentifiers": "PRD1, PRD2, PRD3" // Its not json array 

@ApiModelProperty(position = 2, example = "[\"PRD1\", \"PRD2\", \"PRD3\"]") 
// This generates -> "productIdentifiers": "[\"PRD1\", \"PRD2\", \"PRD3\"]" // Its too not json array 

有什麼辦法,我可以生成列表正確例如值財產?

更新:

我已經試過

@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3") 
private List<String> productIdentifiers; 
//This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"` 

更新2通過@nullpointer和@Zeeshan阿里夫建議的解決方案:

嘗試下面的方法,其沒有產生適當的迴應

@ApiModelProperty(position = 2, dataType="java.util.List<String>", example = "PRD1, PRD2, PRD3") 
// This generates -> "productIdentifiers": "PRD1, PRD2, PRD3" 


@ApiModelProperty(position = 2, dataType="String[]", example = "PRD1, PRD2, PRD3") 
// This generates -> "productIdentifiers": "PRD1, PRD2, PRD3" 

我的招搖罐子Maven的依賴是:

<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger2</artifactId> 
    <version>2.5.0</version> 
    <exclusions> 
     <exclusion> 
      <artifactId>mapstruct</artifactId> 
      <groupId>org.mapstruct</groupId> 
     </exclusion> 
    </exclusions> 
</dependency> 

更新 github ticket for this issue

+0

請標記我的答案。謝謝 –

+0

@Anil Bharadia你對'dataType = List 「'有什麼反應?'也只是爲了精確測試,你使用了什麼樣的swagger版本/依賴關係? – nullpointer

+0

@nullpointer對於所有這些,我得到了」PRD1,PRD2,PRD3「我的嘗試 –

回答

2

嘗試初始化@ApiModelProperty如下:

public class MyClass { 
    .... 
    @ApiModelProperty(
     position = 2, datatype="List", example = "PRD1, PRD2, PRD3" 
    ) 
    private List<String> productIdentifiers; 
    .... 
} 
+1

感謝您的建議,但這是行不通的 –

+0

你能分享詳細信息嗎 –

+0

我已更新詳細信息如果你需要更多的細節請告訴我 –

-1

你只需要使用Reflection符號。使用

@ApiModelProperty(dataType = "[Ljava.lang.String;") 

工作正常,但我不能把例子。

這是結果:

{ 
    "field": [ 
    "string" 
    ] 
} 
+0

嗨丹尼爾,是的,它產生所需格式的輸出,但問題是產生具有示例值的輸出 –

3

TLDR:一上揚鞭-API的貢獻者已經對這個功能了合作,在3.0.0版本中添加這一點,但它尚無法確定何時會被釋放。現在它站在Swagger-API的功能/ 3.0.0-rc2分支上GitHub

我一直在Swagger工作近兩個月,並且隨着我們的項目進展問題出現。現在我做了一些研究,並在Switger-API的GitHub頁面上閱讀,該功能根本無法使用(還)。

如上所述here[這裏將是一個其他的聯繫,但我的名聲不夠高後超過2個鏈接]自2015年8月這個功能已經被要求幾次沒有多少運氣。

現在上this issue on the Swagger-API github,貢獻者之一說:

這需要該機型的一大重構,這是在路上。 2017年3月3日

從而導致後來的註釋:

將在3.0.0支持的支持,請詳見功能/ 3.0.0-RC2分支。 2017年

而在2017年8月9日有人問時的3.0.0版本的發佈將是一個沒有進一步的迴應6月27日。

因此,總之,支持數組/列表的示例已得到處理,應在3.0.0版中提供,但沒有關於何時發佈的消息。

+0

令人印象深刻的分析,真的。 – Gangnus

0

一個醜陋的解決辦法,直到我們有這個功能適當的支持,其產生的例子是隻有一個元素列表,但至少可以讓使用allowableValues展現的東西不僅僅是"string"更有用:

@ApiModelProperty(position = 2, allowableValues = "PRD1") 
// This generates -> "productIdentifiers": ["PRD1"]