欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Springboot集成接口文档swagger,使用,测试

程序员文章站 2022-09-26 21:10:49
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新 ......

springboot 配置接口文档swagger

1. swagger2 介绍

1.1 简介

swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 restful 风格的 web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。

随着前后端技术的日渐成熟,前后端的交互就只有接口了,前端请求接口获取数据,所以接口的格式化也就相当重要,有一个标准格式的接口文档在开发过程中是相当重要的,swagger就是这么一个在线的接口文档,在springboot的集成之中也相当便利。

swagger可以自动生成在线接口文档,界面可视化的同时保证了便利的测试接口。

2. maven 配置swagger2依赖

创建一个springboot web 项目,然后在pom.xml中添加如下依赖:

<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>

可以根据自己的sringboot版本适当降低swagger2 的版本。

3. swagger2 配置

在springboot启动类的同级目录下面创建一个config的包,然后创建一个配置swagger2 的配置类。

package com.example.demoswagger.config;

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import springfox.documentation.builders.apiinfobuilder;
import springfox.documentation.builders.pathselectors;
import springfox.documentation.builders.requesthandlerselectors;
import springfox.documentation.service.apiinfo;
import springfox.documentation.spi.documentationtype;
import springfox.documentation.spring.web.plugins.docket;
import springfox.documentation.swagger2.annotations.enableswagger2;

/** 
 * @author 全栈学习笔记
 * @date 2020/4/19 16:00
 * @description
 */
@configuration
@enableswagger2
public class swaggerconfig {
    @bean
    public docket createrestapi() {
        return new docket(documentationtype.swagger_2)
                // 指定构建api文档的详细信息的方法:apiinfo()
                .apiinfo(apiinfo())
                .select()
                // 指定要生成api接口的包路径
                .apis(requesthandlerselectors.basepackage("com.example.demoswagger.controller"))
                //使用了 @apioperation 注解的方法生成api接口文档
                //.apis(requesthandlerselectors.withmethodannotation(apioperation.class))
                .paths(pathselectors.any())
                //可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }

    /**
     * 设置api文档的详细信息
     */
    private apiinfo apiinfo() {
        return new apiinfobuilder()
                // 标题
                .title("spring boot集成swagger2")
                // 接口描述
                .description("swagger")
                // 联系方式
                .contact("微信公众号"+"全栈学习笔记"+"359076197@qq.com")
                // 版本信息
                .version("1.0")
                // 构建
                .build();
    }
}

注意其中的包,不要导错包了。

配置代码说明:

1. 使用 @configuration 注解,标识这是一个配置类,项目启动的时候会自动调用加载,@enableswagger2 注解的作用是自动开启swagger2。

2. apiinfo() 方法里面的参数可以自己设定,在第一个方法中,我们除了可以根据接口所在的包对应生成接口文档还可以根据项目中是否有方法使用了 @apioperation注解来判断是否生成api文档。

4. controller 测试编写以及注解说明

上面我们配置好了swagger api生成的配置之后就可以编写测试的controller,创建一个与config同级的包controller,然后里面写一个testcontroller

单个参数

@restcontroller
@requestmapping("/test")
@api("测试swagger接口")
public class testcontroller {

    @requestmapping(path = "/getstudent",method = requestmethod.get)
    @apioperation("/根据学生id获取学生信息")
    @apiimplicitparam(name = "id",value = "id",required = true,paramtype = "query",datatype = "int")
    public student getstudent(@requestparam integer id){
        student student = new student();
        student.setid(11);
        student.setage(21);
        student.setname("全栈学习笔记");
        map<integer,student> studentmap = new hashmap<>();
        studentmap.put(11,student);
        return studentmap.get(id);
    }
}

其中,student类等会会贴出来。

代码说明:

  1. @restcontroller 相当于@controller+@responsebody 注解,让标识的这个类返回json格式的数据。
  2. 上面加上@api的注解,说明这个类要生成api文档,并给予描述。相当于可以根据这个类作为类别的划分。在类里面的方法加上@apioperation 注解 用来描述这个方法(接口)是用来干嘛的;
  3. @apiimplicitparam 注解是为了描述这个方法之中的参数。其中的name,value属性你应该知道。required 属性是标识在测试接口时,这个参数是否需要传,true为必须传,false为非必须。
  4. @apiimplicitparam之中的paramtype是标识这个参数应该在哪去获取,常用的有以下几种
  • header-->放在请求头。请求参数的获取注解:@requestheader
  • query -->常用于get请求的参数拼接。请求参数的获取注解:@requestparam
  • path -->(用于restful接口)-->请求参数的获取获取注解:@pathvariable
  • body -->放在请求体。请求参数的获取注解:@requestbody
  1. @apiimplicitparam之中的datatype 是用来标识这个参数的类型,默认为string,如果是数组类型,需要加上allowmultiple=true,表示是数组类型的数据。也可以是自定义的对象类型。

多个参数的用法

@requestmapping(path = "/getstudent",method = requestmethod.patch)
@apioperation("/根据学生id获取学生信息")
@apiimplicitparams({
    @apiimplicitparam(name = "name",value = "姓名",required = true,paramtype = "query",datatype = "string"),
    @apiimplicitparam(name = "age",value = "年龄",required = true,paramtype = "query",datatype = "int")
})
public student editstudent(@requestparam string name, @requestparam integer age){
    student student = new student();
    student.setid(12);
    student.setname(name);
    student.setage(age);
    return student;

}

需要对多个参数进行属性说明时,需要用到 @apiimplicitparams,然后里面再用 @apiimplicitparam

参数是对象的用法

controller代码

 @apioperation("/添加学生信息")
    @requestmapping(path = "/addstudent",method = requestmethod.post)
    public map<integer,student> addstudent(@requestbody student student){
        map<integer,student> studentmap = new hashmap<>();
        studentmap.put(student.getid(),student);
        return studentmap;
    }

entity代码:

/**
 * (student)实体类
 *
 * @author 微信公众号:全栈学习笔记
 * @since 2020-04-14 11:39:10
 */
@apimodel("学生类")
public class student {

    /**
    * 唯一标识id
    */
    @apimodelproperty("id")
    private integer id;
    /**
    * 姓名
    */
    @apimodelproperty("姓名")
    private string name;
    /**
    * 年龄
    */
    @apimodelproperty(value = "年龄")
    private integer age;
}
&emsp;省略get,set方法
&emsp;@apimodelproperty 的属性配置相对之前那个@apiimplicitparam的属性更多更全。

5. 接口测试

完成以上步骤,带你们看看swagger的界面如何,启动项目,浏览器输入localhost:8091/swagger-ui.html
如下:
Springboot集成接口文档swagger,使用,测试

打开之后
Springboot集成接口文档swagger,使用,测试

测试一个接口
Springboot集成接口文档swagger,使用,测试

结果返回
Springboot集成接口文档swagger,使用,测试

6.总结

本期的分享就到这里,文中讲解了swagger2的配置,用法,以及测试,整个流程都讲解的较详细。如果你觉得文章有用,点个赞吧!

转自:https://www.cnblogs.com/swzx-1213/p/12736993.html