1
votes

In my spring boot application, I have multiple Rest Controllers and need to generate swagger for each controller seperately.

By using below Docket config for each controller in my spring boot application class, i am able to download controller specific swagger by going to /v2/api-docs?group=ai where i = 1 to n

However in swagger-ui.html, when i select a1(/v2/api-docs?group=a1), it shows path as "/api/a1/a1", while selecting a2(/v2/api-docs?greoup=a2), it shows correct path i.e. /api/a2

I have tried changing in Docket ,paths regex to absolute e.g. "api/a1" etc but that didn't help.

@Bean
public Docket a1Api() {
    return new Docket(DocumentationType.SWAGGER_2)
    .groupName("a1")
    .apiInfo(a1Info())
    .select().apis(RequestHandlerSelectors.any())
    .paths(regex("/api/a1.*"))
    .build()
    .pathMapping("/");
}

@Bean
public Docket a2Api() {
    return new Docket(DocumentationType.SWAGGER_2)
    .groupName("a2")
    .apiInfo(a1Info())
    .select().apis(RequestHandlerSelectors.any())
    .paths(regex("/api/a2.*"))
    .build()
    .pathMapping("/");
}

private ApiInfo a1Info() {
    return new ApiInfoBuilder()
    .title("a1 Swagger 2.0")
    .description("a1")
    .license("a1")
    .version("1.0")
    .build();
}

private ApiInfo a2Info() {
    return new ApiInfoBuilder()
    .title("a2 Swagger 2.0")
    .description("a2")
    .license("a2")
    .version("1.0")
    .build();
}

Rest Controllers

@RestController
@Api(tags = "A1")
@RequestMapping("/api/a1")
public class a1Controller {

        @ApiOperation(value = "a1")
        @RequestMapping(value = "", method = RequestMethod.POST)
        public a1Response invoke(@RequestBody a1Request va1Request) {
            .....;
        }
}

@RestController
@Api(tags = "An")
@RequestMapping("/api/an")
public class a1Controller {

        @ApiOperation(value = "an")
        @RequestMapping(value = "", method = RequestMethod.POST)
        public anResponse invoke(@RequestBody anRequest vanRequest) {
            .....;
        }
}

Any idea how can i address this....
i am using springfox swagger version 2.6.1

2
Cannot really make out. Based on what I see it should work as expected. If you're still having issues I'd recommend creating an issue.Dilip Krishnan
It is working fine, issue was i moved Request Mapping at controller level and forgot to move from method level, so was showing strange path plus also removed pathMapping from docket.chappalprasad

2 Answers

2
votes

You can add multiple controller class using following Swagger Configuration:

1) Create a Swagger Configuration Class.

2) Then specify the base package of controllers.

import java.util.Collections;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig
{

  private static final ApiInfo DEFAULT_API_INFO = null; //Swagger info

  @Bean
  public Docket api() 
  {
    return new Docket(DocumentationType.SWAGGER_2)
            .forCodeGeneration(Boolean.TRUE)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.user.controller"))
            .paths(PathSelectors.any())
            .paths(Predicates.not(PathSelectors.regex("/logout.*")))
            .build()
            .apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
     return new ApiInfo(
       "REST API", 
       "REST description of API.", 
       "API TOS", 
       "Terms of service", 
       new Contact("Rajib Garai", "https://www.linkedin.com/in/rajibgarai90/", "[email protected]"), 
       "License of API", "API license URL", Collections.emptyList());
}
}
1
votes

Here is the code i wrote to find and automatically create Docket on runtime per controller, also has a Default Docket to show all in one group.

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    ConfigurableApplicationContext context;

    //Default Docket to show all
    @Bean
    public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(metaData())
        .forCodeGeneration(Boolean.TRUE)
        .select()
        .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
        .paths(PathSelectors.any())
        .paths(Predicates.not(PathSelectors.regex("/error.*")))
        .build();
    }

    //Creating Docket Dynamically per Rest Controller 
    @PostConstruct
    public void postConstruct() throws ClassNotFoundException {
    ClassPathScanningCandidateComponentProvider provider
        = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
    for (BeanDefinition beanDef : provider.findCandidateComponents("com.blah.blah.package")) {
        Class<?> cl = Class.forName(beanDef.getBeanClassName());
        RequestMapping requestMapping = cl.getAnnotation(RequestMapping.class);
        if (null != requestMapping && null != requestMapping.value() && requestMapping.value().length > 0) {
        String resource_group = requestMapping.value()[0];
        SingletonBeanRegistry beanRegistry = context.getBeanFactory();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .groupName(resource_group)
            .apiInfo(metaData())
            .forCodeGeneration(Boolean.TRUE)
            .select()
            //.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .paths(PathSelectors.regex(resource_group + ".*"))
            .paths(Predicates.not(PathSelectors.regex("/error.*")))
            .build();
        beanRegistry.registerSingleton(cl.getSimpleName() + "_docket_api", docket);
        }
    }
    }

    private ApiInfo metaData() {
    return new ApiInfoBuilder()
        .title("some Title Here")
        .description("Some Desciption")
        .version("1.0")
        .contact(new Contact("Asad Abdin", "", "[email protected]"))
        .build();
    }