Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom annotation in swagger2 using with Springfox?

i am using swagger2 and i want to create new @ApiSepecificationInfo annotation and this should be consider in auto generator of swagger2 doc(like if i hit Gradlw generatordoc)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiSpecificationInfo {
   String name();
   String description();
}

please let me is this possible or not ?

like image 605
Akash Namdev Avatar asked Jun 05 '17 12:06

Akash Namdev


People also ask

What is Springfox Swagger2?

React Full Stack Web Development With Spring Boot Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.

How does Springfox work?

Springfox works by examining an application, once, at runtime to infer API semantics based on spring configurations, class structure and various compile time java Annotations.

Does Springfox support OpenAPI 3?

In mid-2020 the version 3.0 of SpringFox was released and it supports Spring 5 and OpenAPI 3.


1 Answers

You can do custom annotation processing by implementing springfox plugins.

If you implement the OperationBuilderPlugin interface springfox is providing you with all information you need.

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
public class OperationBuilderPluginImpl implements OperationBuilderPlugin {

    @Override
    public void apply(OperationContext context) {
        Optional<ApiOperation> methodAnnotation = context.findAnnotation(ApiSpecificationInfo.class);
        if (methodAnnotation.isPresent()) {
            ApiSpecificationInfo apiSpecificationInfo = methodAnnotation.get();
            // do your processing here
            context.operationBuilder().notes(apiSpecificationInfo.name());
        }
    }

    @Override
    public boolean supports(DocumentationType delimiter) {
        return SwaggerPluginSupport.pluginDoesApply(delimiter);
    }
}

See github for reference.

like image 62
BesirKongfu Avatar answered Oct 14 '22 03:10

BesirKongfu