无法将Springfox Swagger2集成到旧版非Spring Boot应用程序中

时间:2019-02-19 09:36:13

标签: java spring spring-boot swagger swagger-ui

我需要在不启动Spring的情况下将swagger2快速集成到旧版Spring MVC项目中。由于现有的spring配置基于xml,因此我首先尝试像这样配置它:

  <!-- Enables swgger ui-->
  <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
  <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

  <!-- Include a swagger configuration-->
  <bean name="applicationSwaggerConfig" class="com.api.SwaggerConfig"/>

SwaggerConfig:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

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

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("API")
            .description("API")
            .build();
  }

}

ResourceConfig:

@Configuration
@EnableWebMvc
  public class SwaggerResourceConfig extends WebMvcConfigurerAdapter {

@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/");
 }
}

我还进行了控制器扫描(我有> 30个RequestMappings):

<context:component-scan base-package="com.api.controller" />  

当我运行它时,v2 / api-docs响应中以及swagger UI中都没有路径。我也尝试只使用java config-结果相同。除了上面的代码之外,Google搜集没有给我任何有关如何将swagger集成到旧版非Spring Boot项目中的指导。知道我在做什么错吗?

0 个答案:

没有答案