隐藏

Swagger Springboot Idea 基础配置(一)

发布:2022/3/10 16:04:42作者:管理员 来源:本站 浏览次数:738

什么是Swagger???

  自己百度官方定义,我目前使用到的,通过类似于注释的配置注解,自动生成一个整齐的页面展示,方便对接前端,同时可以页面测试的巨方便的一个好东西.
使用方法
  1、配置pom文件

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

  2、springBoot整合swagger

  配置Swagger参数

    /**
     * Swagger配置
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
     
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)//
                    .groupName("研发测试")//
                    .apiInfo(apiInfo())//
                    .select()//
                    .apis(RequestHandlerSelectors.basePackage("com.study.blog"))//添加ApiOperiation注解的被扫描
                    .paths(PathSelectors.any())//
                    .build();
        }
     
    /*
        @Bean
        public Docket createRestApi() {
            Docket docket = new Docket(DocumentationType.SWAGGER_2)//
                    .groupName("研发测试")//
                    .apiInfo(apiInfo());
            //页面展示的基本信息
            Docket build = docket.select()
                    .apis(RequestHandlerSelectors.basePackage("com.study.blog.swagger"))//添加ApiOperiation注解的被扫描
                    .paths(PathSelectors.any()).build();
            System.out.println(build == docket);//true
            return build;
        }
    */
     
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()//
                    .title("Swagger Interface")//
                    .description("数据交换格式: JSON")//
                    .license("我自己的测试 2020 © All rights Reserved")//
                    .version("v2.9.0")//
                    .build();
        }
    }

  其中的参数

  --groupName   因为有可能有多个Docket 配置,所以用一个名字区分

  --apiInfo  页面展示的基本信息

接下来的参数如注释写的一样,给配置一个选择器,筛选展示的数据

  --select 是一个选择器,初始化docket的选择

  --apis  这个用来配置swagger扫描的包

  --paths  这个配置符合包里边的路径,可以通过正则匹配,目前是全匹配
3、给方法添加注解

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
     
    /**
     * @author haotonghui <haotonghui@kuaishou.com>
     * Created on 2020-12-15
     */
    @RestController
    @RequestMapping("/swagger/test")
    @Api(tags = "SwaggerTest", description = "swagger展示的接口调用")
    public class SwaggerTestController {
     
     
        @GetMapping("getTest")
        @ApiOperation(value = "获取信息")
        public String getTest() {
            return "信息";
        }
     
        @PostMapping("postTest")
        @ApiOperation(value = "新增信息")
        public String postTest() {
            return "新增信息";
        }
     
     
    }

4、访问本地链接

  http://localhost:8080/swagger-ui.html  在swagger的2版本的生成网页的规则是, IP:端口/项目虚拟目录/swagger-ui.html
5、效果

配置文件中所提到的信息,都有展示

 

类中的详细注解api详解请看下一章:Swagger Springboot Idea 基础配置(二)