如何从对象中提取参数以在文档中的参数中显示

时间:2017-01-04 15:52:33

标签: java swagger swagger-ui swagger-2.0

我有以下API端点:

@ApiResponses(
    value = {
        @ApiResponse(code = 200, message = "OK",
            responseHeaders = {
                @ResponseHeader(name = "X-RateLimit-Limit", description = "The defined maximum number of requests available to the consumer for this API.", response = Integer.class),
                @ResponseHeader(name = "X-RateLimit-Remaining", description = "The number of calls remaining before the limit is enforced and requests are bounced.", response = Integer.class),
                @ResponseHeader(name = "X-RateLimit-Reset", description = "The time, in seconds, until the limit expires and another request will be allowed in. This header will only be present if the limit is being enforced.", response = Integer.class)
            }
        )
    }
)
@ApiOperation(httpMethod = "GET", hidden = false, nickname = "Get Network Availability in JSON", value = "Get network availability for a product", response = AvailableToPromise.class, position = 1)
@RequestMapping(value = "/{product_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> networkAvailabilityJsonResponse(
    @RequestHeader HttpHeaders headers,
    @PathVariable("product_id") String productId,
    @Valid NetworkAvailabilityCmd cmd,                  //query params
    BindingResult result)
    throws Exception {}
}

某些参数(例如key)从查询中获取并通过Spring MVC映射到此对象。

然而,在我的swagger-ui端点的参数部分中,它向我展示了一些奇怪的东西:

NetworkAvailabilityCmd中的所有变量都不显示在此参数列表中,而cmd本身显示为位于请求正文中(它实际位于查询中)。有没有办法隐藏cmd并提取此对象内的参数以显示在参数列表中?我希望params列表看起来像这样(有更多参数):

如果我在方法端点上使用@ApiImplicitParams,并且写出每个参数,我就能做到这一点。但是,此NetworkAvailabilityCmd用于许多端点,并且每个端点上的params列表非常混乱。能够从对象中提取变量将更加清晰,并且会阻止人们忘记将整个列表添加到新端点。

我想它需要NetworkAvailabilityCmd cmd上的注释,并且可能是该类中变量的内容,但我似乎无法找到我在文档中寻找的内容。

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现添加@ModelAttribute神奇地工作了。这个注释来自Spring。