3

I have a Springboot Rest application where I have annotation which is automatically converted API's and Params.

I have custom annotation where i include some notes to it, How can i get that generated to my swagger page in OpenAPI 3?

Ex:
@RestController
Class Controller {

@GetMapping(/test/result/)
@CustomAnnotation(value = "This description should come in swagger")
void method() {
}
}
Muthukumar
  • 93
  • 2
  • 3
  • 10

2 Answers2

5

SpringDoc allows you to customize the generated OpenAPI specification by implementing your own Customizer bean.

There are plenty of Customizer interfaces that you can use for customization, but the most usable are OperationCustomizer, ParameterCustomizer, and PropertyCustomizer.

Below is an example of Operation Customizer for your use case.

@Component
public class OperationCustomizer implements org.springdoc.core.customizers.OperationCustomizer {
    @Override
    public Operation customize(Operation operation, HandlerMethod handlerMethod) {
        CustomAnnotation annotation = handlerMethod.getMethodAnnotation(CustomAnnotation.class);
        if (annotation != null) {
            operation.description(annotation.value());
        }
        return operation;
    }
}

Here you can find an example of the project that uses custom annotations and Customizers for them.

And here an example of the project that modifies the generated specification based on the @NonNull annotation.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
VadymVL
  • 5,366
  • 3
  • 26
  • 41
0

As @VadymVL pointed out a component extending OperationCustomizer is necessary:

@Component
public class CustomOperationCustomizer implements OperationCustomizer {
  @Override
  public Operation customize(Operation operation, HandlerMethod handlerMethod) {
      /* your code */
  }

Just don't forget to register it:

@Bean
  public GroupedOpenApi publicApi(CustomOperationCustomizer operationCustomizer) {
    return GroupedOpenApi.builder()
            .group(/*your group*/)
            .pathsToMatch(/*your endpoint*/)
            .
            /* anything else you need */
            .
            .addOperationCustomizer(operationCustomizer)
            .build();
  }

And you're good to go!

ferrouskid
  • 466
  • 2
  • 7