springboot整合swagger的3步骤

springboot整合swagger

文章目录

    • 1. 添加swagger依赖
    • 2. 添加swagger配置类
    • 3. 启动类上添加注解
    • 参考文章

1. 添加swagger依赖

 
        
            io.springfox
            springfox-swagger2
            2.8.0
        
        
            io.springfox
            springfox-swagger-ui
            2.8.0
        

2. 添加swagger配置类

@Configuration
    public class Swagger2Configuration {
        /**
         * 对所有api扫描配置:controller路径
         */
        private static final String BASE_PACKAGE = "org.mm.controller";
        /**
         * Swagger2的配置文件:内容、扫描包等
         *
         * @return the docket
         */
        @Bean
        public Docket createApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
                    .paths(PathSelectors.any())
                    .build();
        }
        @Bean
        public RequestMappingInfoHandlerMapping requestMapping() {
            return new RequestMappingHandlerMapping();
        }
        /**
         * 构建api文档的详细方法
         *
         * @return ApiInfo
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    //页面标题
                    .title("Spring mvc 测试使用 Swagger2 构建Api")
                    //创建
                    .termsOfServiceUrl("http://www.baidu.com")
                    .version("1.0.0")
                    //描述
                    .description("API 描述")
                    .build();
        }
    }

3. 启动类上添加注解

springboot整合swagger的3步骤_第1张图片

参考文章

  1. 微服务之Swagger https://www.cnblogs.com/softidea/p/6251249.html

你可能感兴趣的:(swagger,springboot)