2
votes

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?

1
What library are you using? Are you using springfox? - Dilip Krishnan
@DilipKrishnan thanks for your reply. Yes, 1.0.2 version. It's old name is swagger-springmvc - LoGary
Its a lot simpler to do with the 2.0 version Its still in snapshot status, but we plan on releasing shortly. We're still working on the documentation but someone will be available at the gitter chat room to help out. - Dilip Krishnan

1 Answers

1
votes

Try this:

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

}