简体   繁体   中英

Customize swagger annotation in spring mvc

I am happy with using swagger to generate API documentation for the front-end developer.

But if I have some methods that need to have a Bearer token or something else in the header of the requests. Problem comes out that I have to repeatedly Copy&Paste the whole annotation on each method. It violates DRY principal and when I have to make some changes on the Bearer token documentation, it will be a disaster.

Current

@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(){};     

What I want to do is create an annotation @ApiOauth2 which is inherited from @ApiImplicitParam(name="Authorization",value = "Bearer token",dataType = "string", paramType ="header") and can be identified by swagger

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

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

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

I searched that annotation can not be extended, how can I achieve such approach?

Try this:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM