Akash Namdev
Akash Namdev

Reputation: 63

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 ?

Upvotes: 5

Views: 2417

Answers (1)

BesirKongfu
BesirKongfu

Reputation: 86

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.

Upvotes: 6

Related Questions