在Swagger文档中传递可接受的字符串值

时间:2018-01-30 22:26:01

标签: spring spring-boot swagger

Spring Boot(Java)。 Swagger是否有任何可自定义/可配置的字段,允许您指定端点接受或返回的特定String值?

例如,我可能有一个像这样的端点:

POST /v1/{accountId}/preferences
{
    "notificationTypes" : [

    ]
}

...在其请求实体中接受notificationTypes数组。也许我的服务器只允许AdminNotificationSimpleAlert作为此notificationTypes的可能值,这意味着:

POST /v1/{accountId}/preferences
{
    "notificationTypes" : [
        "SimpleAlert"
    ]
}

有效,但是:

POST /v1/{accountId}/preferences
{
    "notificationTypes" : [
        "Hello"
    ]
}

抛出400 Bad Request。我希望能够在我的Swagger文档中进行沟通。 是否可以通过注释进行此配置?

2 个答案:

答案 0 :(得分:1)

我认为一个好的解决方案是创建一个具有可接受值的ENUM。这样,它将在Swagger中自动描述:

@ApiModelProperty(value= "Accepted values are :")
public NotificationTypes notificationTypes;


public enum NotificationTypes {SimpleAlerts, AdminNotifications}

在生成的规范中,它提供以下内容:

enter image description here

玩得开心!

答案 1 :(得分:0)

您可以使用-

  

notificationTypes:
    类型:字符串
     例如:[“ SimpleAlert”,“ otherValue”]

外观-https://gyazo.com/1c8774fbf08410c19df0506a52a767d6
ref-https://swagger.io/docs/specification/adding-examples/