首页 文章

使用自定义转换器的Spring Boot打破了Swagger以及如何移动它

提问于
浏览
4

我有一个非常简单的Spring Boot应用程序 . 我通过基本 SpringApplication.run(Startup.class, args); 启动它,并在那里有一个自定义 @Configuration 类来覆盖默认转换器 . 我决定将Swagger添加到各种东西中,以便我可以为内部用户群生成更好的文档,因为有大量的 endpoints .

当我开始做事时,Swagger根本行不通 .

我决定用一个 endpoints 启动一个前端临时Spring Boot来记录出错的地方 . Out-of-the box this worked perfectly fine 并且我能够通过点击http://localhost:8080/swagger-ui.html基本URL来运行Swagger .

当我实现扩展 WebMvcConfigurationSupport 的自定义 @Configuration 类时,Swagger不再有效 .

我的配置覆盖了一个方法:

@Configuration
public class StartupConfiguration extends WebMvcConfigurationSupport {

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(getJsonConverter());
  }
}

而已 . 我决定添加默认转换器,没有任何运气 . 然后我只是清空了课程并将其保留为:

@Configuration
public class StartupConfiguration extends WebMvcConfigurationSupport {
}

Swagger仍然破碎 - 如果我 remove the class completely ,那么它有效 .

如何保留自定义配置数据并运行Swagger?我也希望将它移动到像http://localhost:8080/swagger/这样的东西,而不是它使用的默认文件,但现在这是一个完全独立的问题 .

我推出的Swagger看起来像这样:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

  @Bean
  public Docket getDocket() {
    // ...
    return new Docket(DocumentationType.SWAGGER_2)
      .groupName("Core API")
      .apiInfo(infoBuilder.build())
      .select().paths(PathSelectors.regex("/*"))
      .build();
  }
}

1 回答

  • 3

    覆盖默认资源处理程序对我有用 . 我将以下内容添加到扩展WebMvcConfigurationSupport的配置类中:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry
                .addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    

相关问题