LoGary
LoGary

Reputation: 322

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?

Upvotes: 2

Views: 1670

Answers (1)

Suraj Bunde
Suraj Bunde

Reputation: 11

Try this:

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

}

Upvotes: 1

Related Questions