简体   繁体   中英

Spring boot reusable springdoc-openapi parameter annotations

I have few controller classes that all requires a header param. To document swagger I am adding this @Parameter annotation to all my endpoints:

    @Parameter(in = ParameterIn.HEADER, description = "some description", name = "some name", content = @Content(schema = @Schema(allowableValues = {VALUE1, VALUE2, VALUE3, VALUE4})))
    @GetMapping
    public void method(@RequestHeader .....) {
    //some code here
    }

Problem is I don't like the idea of repeating the same annotation all over the controller methods. Is there a nice clean solution to have a reusable annotation here?

So partial fix is create my own custom interface like that:

@Target({PARAMETER, METHOD, FIELD, ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Parameter(in = ParameterIn.HEADER, description = "some description", name = "some name", content = @Content(schema = @Schema(allowableValues = {VALUE1, VALUE2, VALUE3, VALUE4})))
public @interface MyCustomAnnotation {
}

that way I can reuse that and save some code. Problem is when some values differ. For example if allowableValues differ between endpoints I can't reuse that one. I can't figure out how can I pass some arguments to my custom annotation to override the default values...

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