zoukankan      html  css  js  c++  java
  • Springboot集成swagger

    一、导入swagger所需maven依赖包

            <!---swagger-ui-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.6.1</version>
            </dependency>
    
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.6.1</version>
            </dependency>

    二、wagger配置类

    如图

    @Configuration
    public class Swagger2 {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.volunteer.sys.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("springboot利用swagger构建api文档")
                    .description("简单优雅的restfun风格,http://blog.csdn.net/saytime")
                    .termsOfServiceUrl("http://blog.csdn.net/saytime")
                    .version("1.0")
                    .build();
        }
    }

    特别要注意的是里面配置了api文件也就是controller包的路径,不然生成的文档扫描不到接口。

    三、Application.class 加上注解@EnableSwagger2 表示开启Swagger

    @Controller
    @SpringBootApplication
    @EnableSwagger2
    @MapperScan(basePackages = "com.volunteer.sys.dao")
    public class VolunteerHelpApplication {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
    
        public static void main(String[] args) {
            SpringApplication.run(VolunteerHelpApplication.class, args);
        }
    
    }

    四、在浏览器打开http://localhost:8080/swagger-ui.html#/

     结束

  • 相关阅读:
    简洁又漂亮的单网页404页源码(html格式404源码)
    运行bee run之后出现的错误以及解决方法
    window beego 安装出现的错误
    golang gin框架 使用swagger生成api文档
    go语言切片作为函数参数
    Go中函数接收器不能改变接收者的地址
    docker 删除none镜像
    redis下载安装
    git切换分支
    angular自定义验证器添加入模板驱动表单
  • 原文地址:https://www.cnblogs.com/zHpx/p/9003196.html
Copyright © 2011-2022 走看看