Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 710
Maciej Avatar asked Dec 09 '25 08:12

Maciej


1 Answers

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...

like image 58
Maciej Avatar answered Dec 10 '25 23:12

Maciej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!