• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringBoot-25-SpringBoot整合Swagger2以和Swagger-Bootstrap-Ui的使用

武飞扬头像
springboot葵花宝典
帮助1

SpringBoot-25-SpringBoot整合Swagger2以及Swagger-Bootstrap-Ui的使用

等等方式吧。但是当我们要和前端开发进行交互的时候我们不仅仅需要自己测试通过,同时还需要给前端工程师这个API接口的开发文档,以及每次测试修改接口后,要对文档进行维护,这就增加了我们

程序开发的成本,并且经常文档维护维护就中断了,那么它就失去了参考价值,变得没有意义。Swagger的出现就是为了解决这些痛点的。个人认为Swagger有以下优点:

  • Swagger提供了API接口文档,并且将代码维护和文档修改融为一体,减少创建文档的麻烦

  • Swagger提供了前端界面用来测试每一个API接口

  • 跨语言,支持多国语言

那么现在我们开始进行Swagger的代码之旅吧。

学新通

引入Swagger的依赖

  1.  
       <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  2.  
            <dependency>
  3.  
                <groupId>io.springfox</groupId>
  4.  
                <artifactId>springfox-swagger2</artifactId>
  5.  
                <version>2.9.2</version>
  6.  
            </dependency>
  7.  
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  8.  
            <dependency>
  9.  
                <groupId>io.springfox</groupId>
  10.  
                <artifactId>springfox-swagger-ui</artifactId>
  11.  
                <version>2.9.2</version>
  12.  
            </dependency>

创建Swagger配置类

创建一个swagger的config配置代码如下:

  1.  
    @Configuration
  2.  
    @EnableSwagger2
  3.  
    public class Swagger2Config {
  4.  
     
  5.  
        @Bean
  6.  
        public Docket api() {
  7.  
            return new Docket(DocumentationType.SWAGGER_2)
  8.  
                    .enable(true)
  9.  
                    .apiInfo(apiInfo())
  10.  
                    .select()
  11.  
                    // 扫描需要生成API文档的controller所在的包路径
  12.  
                    .apis(RequestHandlerSelectors.basePackage("com.learn.springboot.controller"))
  13.  
                    //为有@Api注解的Controller生成API文档
  14.  
    //                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
  15.  
                    //为有@ApiOperation注解的方法生成API文档
  16.  
    //                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
  17.  
                    .paths(PathSelectors.any())
  18.  
                    .build();
  19.  
        }
  20.  
     
  21.  
        private ApiInfo apiInfo() {
  22.  
            return new ApiInfoBuilder()
  23.  
                    .title("Spring Boot 2.x集成Swagger")
  24.  
                    .description("测试 API 1.0 操作文档")
  25.  
                    .termsOfServiceUrl("test")
  26.  
                    .version("1.0")
  27.  
                    .contact(new Contact("springboot葵花宝典""""test@gmail.com"))
  28.  
                    .build();
  29.  
        }
  30.  
    }
学新通

  • @Configuration:声明Swagger2Config为配置类

  • @EnableSwagger2:声明配置Swagger配置

配置Swagger的测试API

经过上面的操作后,Swagger页面以及可以看到基本信息了,但是我们还没有添加相关的API信息,现在我们就添加相关的API操作,代码如下:

Student实体类

  1.  
    @Data
  2.  
    @ApiModel(value = "学生实体类",description = "学生信息用作增删改查")
  3.  
    @JsonPropertyOrder(value={"name","mobile","sex"})
  4.  
    public class Student implements Serializable{
  5.  
     
  6.  
        @ApiModelProperty(value = "学生ID", name = "id", required = false, example = "0")
  7.  
        @JsonIgnore
  8.  
        private Long id;
  9.  
        @ApiModelProperty(value = "学生电话", name = "mobile", required = true,example = "133333")
  10.  
        private String mobile;
  11.  
        @ApiModelProperty(value = "学生性别", name = "性别", required = true,example = "男")
  12.  
        @JsonProperty("性别")
  13.  
        private String sex;
  14.  
        @ApiModelProperty(value = "学生姓名", name = "name", required = true,example = "张三")
  15.  
        private String name;
  16.  
        @ApiModelProperty(value = "学生年龄", name = "age", required = true,example = "20")
  17.  
        private int age;
  18.  
        @ApiModelProperty(value = "邮箱", name = "email", required = true,example = "133@qq.com")
  19.  
        @JsonProperty("邮箱")
  20.  
        private String email;
  21.  
        @ApiModelProperty(value = "是否可用1可用,0不不用", name = "isEnabled", required = false,example = "1")
  22.  
        private int isEnabled;
  23.  
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT 8")
  24.  
        private Date createDate;
  25.  
        private Date updateDate;
  26.  
     
  27.  
    }
学新通

注解

  • ApiModel:作用在实体类上,表示响应数据

  • @ApiModelProperty:实体类属性的描述

添加Controller

  1.  
    @Api(value = "测试Swagger",tags = "测试")
  2.  
    @RestController
  3.  
    @RequestMapping("/test")
  4.  
    public class TestController {
  5.  
        /**
  6.  
         *测试
  7.  
         */
  8.  
        @ApiImplicitParams({
  9.  
                @ApiImplicitParam(name = "name", value = "name", required = false, paramType = "path",dataType = "String",
  10.  
                        example = "张三"),
  11.  
        })
  12.  
        @ApiOperation(value = "测试",tags = "测试",notes = "测试swagger")
  13.  
        @GetMapping("/{name}")
  14.  
        public String test(@PathVariable("name")String name) {
  15.  
            return "Hello :"  name;
  16.  
        }
  17.  
     
  18.  
        /**
  19.  
         *测试student
  20.  
         */
  21.  
        @ApiOperation(value = "测试student",tags = "测试student",notes = "测试student")
  22.  
        @PostMapping("/student")
  23.  
        public Student testStudent(@RequestBody Student student) {
  24.  
            return  student;
  25.  
        }
  26.  
    }
  27.  
     
学新通

注解

  • @Api:作用在Controller类上,value的值默认不显示tag属性为具体描述

  • @ApiOperation:作用在Controller类的方法上

  • @ApiImplicitParams:方法中参数的说明
    • value:参数的说明

    • paramType:参数输出的位置,header(请求参数的获取:@RequestHeader)、query(请求参数的获取:@RequestParam)、path(请求参数的获取:@PathVariable)

    • dataType:参数类型

    • example:参数示例

    • required:参数是否必须

测试

启动项目,然后输入:

http://localhost:8080/swagger-ui.html进行测试。其整体展示效果如下:

学新通

使用Swagger进行测试

我们对测试方法接口将那些测试,点击Try it out

学新通

进入参数页面

学新通

输入name,点击execute,就完成了接口的请求了。

整合swagger-bootstrap-ui

swagger默认的api接口是上下结构,在浏览器中显示不符合我们开发系统的左右菜单风格,swagger-bootstrap-ui就是一套基于左右菜单风格,只需要在pom.xml中添加依赖即可。

  1.  
     <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  2.  
            <dependency>
  3.  
                <groupId>io.springfox</groupId>
  4.  
                <artifactId>springfox-swagger2</artifactId>
  5.  
                <version>2.9.2</version>
  6.  
            </dependency>
  7.  
            <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  8.  
            <dependency>
  9.  
                <groupId>io.springfox</groupId>
  10.  
                <artifactId>springfox-swagger-ui</artifactId>
  11.  
                <version>2.9.2</version>
  12.  
                <exclusions>
  13.  
                    <exclusion>
  14.  
                        <groupId>io.swagger</groupId>
  15.  
                        <artifactId>swagger-annotations</artifactId>
  16.  
                    </exclusion>
  17.  
                    <exclusion>
  18.  
                        <groupId>io.swagger</groupId>
  19.  
                        <artifactId>swagger-models</artifactId>
  20.  
                    </exclusion>
  21.  
                </exclusions>
  22.  
            </dependency>
  23.  
            <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
  24.  
            <dependency>
  25.  
                <groupId>com.github.xiaoymin</groupId>
  26.  
                <artifactId>swagger-bootstrap-ui</artifactId>
  27.  
                <version>1.9.6</version>
  28.  
            </dependency>
  29.  
            <dependency>
  30.  
                <groupId>io.swagger</groupId>
  31.  
                <artifactId>swagger-annotations</artifactId>
  32.  
                <version>1.5.21</version>
  33.  
            </dependency>
  34.  
            <dependency>
  35.  
                <groupId>io.swagger</groupId>
  36.  
                <artifactId>swagger-models</artifactId>
  37.  
                <version>1.5.21</version>
  38.  
            </dependency>
学新通

修改过依赖以后访问Swagger API文档

访问swagger-bootstrap-ui文档

  • swagger文档地址:http://localhost:8080/doc.html

  • swagger效果展示

实体类展示

学新通

方法展示

学新通

如果您觉得本文不错,欢迎关注,点赞,收藏支持,您的关注是我坚持的动力!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfjkag
系列文章
更多 icon
同类精品
更多 icon
继续加载