springboot 不支持具有相同组名的多个 Docket

use*_*190 1 swagger spring-boot springfox

我有以下 swagger 的弹簧引导配置,当服务启动时,我收到以下错误,我不确定为什么会这样。我遵循了一个教程,它对他们有用。

java.lang.IllegalStateException: Multiple Dockets with the same group name are not supported. The following duplicate groups were discovered. default


@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SpringFoxConfig {

  @Bean
  public Docket apiDocket() {
    return new Docket(DocumentationType.SWAGGER_2)
      .select()
      .apis(RequestHandlerSelectors.basePackage("test.rest"))
      .paths(PathSelectors.ant("/test/**"))
      .build()
      .apiInfo(apiInfo());
  }

  // Describe the apis
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
      .title("test")
      .description("Test")
      .version("1.0.0")
      .license("vvv")
      .build();
  }

}
Run Code Online (Sandbox Code Playgroud)

我还有另一个配置

@OurApp
@EnableSwagger2
public class CoreApp extends OurApp {

}
Run Code Online (Sandbox Code Playgroud)

小智 7

在这里,您尝试使用相同的组名执行多个 Docket,这是不可接受的。请查看提供的链接。

groupName(java.lang.String groupName) 如果存在多个 Docket 实例,则每个实例都必须具有此方法提供的唯一 groupName。文档

public class Docket implements DocumentationPlugin {
        public static final String DEFAULT_GROUP_NAME = "default"; 
}
Run Code Online (Sandbox Code Playgroud)

您可以在上面看到 DocumentPlugin 将组名设置为“默认”。

public Docket(DocumentationType documentationType) {
        this.apiInfo = ApiInfo.DEFAULT;
        this.groupName = "default"; 
Run Code Online (Sandbox Code Playgroud)

上面有“默认”作为组名。

因此,您需要为两个 Docket 设置两个不同的组名。

您需要做的就是更改您的代码,如下所示:覆盖现有的默认组名。

@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SpringFoxConfig {
    @Bean
    public Docket apiDocket() {
        String groupName = "Swagger";
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("test.rest"))
                .paths(PathSelectors.ant("/test/**"))
                .build()
                .groupName(groupName)
                .apiInfo(apiInfo());
    }

    // Describe the apis
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("test")
                .description("Test")
                .version("1.0.0")
                .license("vvv")
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)