【发布时间】:2017-05-30 00:15:23
【问题描述】:
在我的 Spring (4.3.2) 项目中,我使用 Swagger (2.7.0) 为我的项目自动生成文档和 swagger-ui。到目前为止效果很好。
但现在我确定我需要能够在控制器级别(而不是方法级别)声明路径变量。而且我需要教 swagger 发现这些路径变量并将它们添加到 docs 和 swagger-ui 中。
我已经创建了自定义注释
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface HasCommonPathVariable {
/**
* The URI template variable to bind to.
*/
String name();
Class<?> type();
String defaultValue() default "";
}
我是这样使用它的:
@RestController
@Secured(SecurityConstants.ROLE_USER)
@RequestMapping(path = "/rest/api/v1/env/{envId}/asset-type")
@HasCommonPathVariable(name = "envId", type = Long.class)
public class AssetTypeRestController extends CustomRestControllerBase<Long, AssetTypeRow, AssetTypeService> {
// ... contorller code
}
我没有使用 Spring 的 PathVariable 注释提及参数的控制器方法,关键是我不允许这样做(这是因为我正在构建微框架)。
所以问题是:如何教 swagger 发现使用控制器级别应用的自定义注释 HasCommonPathVariable 描述的路径变量?
【问题讨论】:
标签: java spring spring-mvc swagger swagger-2.0