首页 > 技术文章 > spring boot整合swagger

zheng-yi 2021-05-19 09:53 原文

  1. 在pom中引入依赖
  <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
        </dependency>
  1. 写一个配置类,可以写成一个公共的工具类
@Configuration//配置类
@EnableSwagger2 //swagger注解
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }
    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://zheng.com", "1123@qq.com"))
                .build();
    }
}
  1. 在启动类上加上包扫描注解,要能够扫描到配置类
@ComponentScan(basePackages = {"xxx.xxx"})
  1. 输入地址 http://localhost:xxxx/swagger-ui.html

推荐阅读