【问题标题】:Customize swagger annotation in spring mvc在spring mvc中自定义swagger注解
【发布时间】:2015-06-21 02:29:35
【问题描述】:

我很高兴使用 swagger 为前端开发人员生成 API 文档。

但是,如果我有一些方法需要在请求的标头中包含不记名令牌或其他内容。问题出现了,我必须在每个方法上反复复制和粘贴整个注释。它违反了 DRY 原则,当我必须对 Bearer 令牌文档进行一些更改时,这将是一场灾难。

当前

@ApiImplicitParam(name="Authorization",value = "Bearer token",dataType = "string", paramType ="header")
public ResponseEntity<Void> doSth(){};

@ApiImplicitParam(name="Authorization",value = "Bearer token",dataType = "string", paramType ="header")
public ResponseEntity<Void> doSth2(){};

@ApiImplicitParam(name="Authorization",value = "Bearer token",dataType = "string", paramType ="header")
public ResponseEntity<Void> doSth3(){};     

我想要做的是创建一个注释 @ApiOauth2 继承自 @ApiImplicitParam(name="Authorization",value = "Bearer token",dataType = "string", paramType ="header")并且可以swagger识别

@ApiOauth2
public ResponseEntity<Void> doSth(){};

@ApiOauth2
public ResponseEntity<Void> doSth2(){};

@ApiOauth2
public ResponseEntity<Void> doSth3(){};

我搜索了注解不能扩展,我该如何实现这种方法?

【问题讨论】:

  • 你用的是什么库?你在用springfox吗?
  • @DilipKrishnan 感谢您的回复。是的,1.0.2 版本。它的旧名称是 swagger-springmvc
  • 2.0 version 的操作要简单得多,它仍处于快照状态,但我们计划很快发布。我们仍在处理文档,但有人会在gitter 聊天室提供帮助。

标签: java spring swagger


【解决方案1】:

试试这个:

 @ApiImplicitParams({
 @ApiImplicitParam(
     name="Authorization",
     value = "Bearer token",
     dataType = "string", 
     paramType ="header")
})
public @interface ApiOauth2 {

}

【讨论】:

    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 2014-09-24
    • 2012-02-25
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多