【问题标题】:How to tell dropwizard-swagger/swagger-ui that there is no request body on a resource method?如何告诉 dropwizard-swagger/swagger-ui 资源方法上没有请求正文?
【发布时间】:2018-05-29 20:34:26
【问题描述】:

我正在将dropwizard-swagger 集成到一个现有的大型项目中。 我现在已经启动并运行了Swagger UI 端点,但我注意到似乎每个方法都必须有一个 body 参数。

具体来说,方法定义中没有@ApiParam 注释的第一个参数被解释为请求正文。似乎没有办法指定正文参数,似乎也没有办法排除参数被Swagger UI 自动标记为这样。这意味着“试用”功能不适用于大部分端点,因为规范不允许主体,但Swagger UI 一直坚持它们存在。

例如,考虑UserResource 文件中的以下方法:

@GET
@Path("v1/users/{userId}/subscriptions")
@ApiOperation(value = "Get user subscriptions", notes = "Returns information about the users current and past subscriptions.")
@UserAccessRequired
@RolesAllowed({//a list of appropriate roles})
@Produces(CompanyMediaType.APPLICATION_API_V1_JSON)
public SubscriptionsDTOV1 getSubscriptionsForUser(@Auth DashboardUser dashboardUser, @JooqInject DSLContext database,
                                                  @Context ResourceContext resourceContext,
                                                  @Context ContainerRequestContext crc,
                                                  @ApiParam(value = "ID of user", type = "Integer") @NotNull @UnwrapValidatedValue @PathParam("userId") IntParam userId) {

Swagger 将第一个参数@Auth DashboardUser dashboardUser 解释为请求正文,并在Swagger UI 中生成以下视图:

Swagger UI with a body parameter

由于这是 GET,因此不允许有正文,并且在测试时尝试删除 Swagger UI 中的正文内容是行不通的,因为该字段会自动填充为 {} .

我如何向Swagger 表明这里没有body 参数并让它工作?仅将 @ApiParam 放在其他方法参数前面是行不通的,因为如果不存在 @QueryParam/@PathParam/etc 注释,则该注释将被忽略。

【问题讨论】:

    标签: swagger swagger-ui dropwizard


    【解决方案1】:

    原来添加 @ApiParam(hidden=true) 似乎已经成功了。我之前试过这个,但没有得到结果,一定是某个地方打错了。

    【讨论】: