Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
838 views
in Technique[技术] by (71.8m points)

api - SpringFox Docket per controller not working in spring boot

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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());
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...