【问题标题】:springboot Multiple Dockets with the same group name are not supportedspring boot 不支持同组名的多个 Docket
【发布时间】:2019-02-27 10:39:01
【问题描述】:

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

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

}

我还有另一个配置

@OurApp
@EnableSwagger2
public class CoreApp extends OurApp {

}

【问题讨论】:

  • 好吧,通过添加一个组名,我修复了它......

标签: spring-boot swagger springfox


【解决方案1】:

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

groupName(java.lang.String groupName) 如果有多个实例 Docket 存在,每个必须有一个唯一的 groupName 由提供 这种方法。 Documentation

public class Docket implements DocumentationPlugin {
        public static final String DEFAULT_GROUP_NAME = "default"; 
}

您可以在上面看到 DocumentPlugin 的 groupname 为“default”。

public Docket(DocumentationType documentationType) {
        this.apiInfo = ApiInfo.DEFAULT;
        this.groupName = "default"; 

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

因此,您需要为两个 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();
    }
}

【讨论】:

  • 其实可以设置new Docket(..).groupName()
【解决方案2】:

好像你给了相同的组名不被接受,请检查并正确设置组名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2014-06-01
    • 2021-05-09
    • 2014-11-12
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多