Springboot集成Swagger(2.9)实现快速接口文档

1.引入maven依赖

        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
  1. 在启动类添加@EnableSwagger2 注解开启swagger扫描
  2. 编写配置类
@Configuration
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig implements WebMvcConfigurer {

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

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.liberty.bms.system.controller")) //扫描包目录
                .paths(Predicates.not(PathSelectors.regex("/error.*"))) //不显示错误接口
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RestFull 数据开放平台 API")
                .version("2.0")
                .description("API描述")
                .build();
    }

}

4.启动访问 http://localhost:8080/swagger-ui.html#/

你可能感兴趣的:(Springboot集成Swagger(2.9)实现快速接口文档)