一、Swagger介绍与使用

一、简介

Swagger 是一款RESTFUL接口的、基于YAML、JSON语言的文档在线自动生成、代码自动生成的工具。

Swagger定义了一套接口规范,通过这套规范,你只需要按照它的规范去定义接口及接口相关的信息。再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种格式的接口文档,生成多种语言的客户端和服务端的代码,以及在线接口调试页面等等。

官网

https://swagger.io/

概述

我将通过以下几点来介绍Swagger这个强大的工具:

  • 注解说明
  • 环境集成
  • Spring Boot 整合Swagger3
  • Spring Boot整合Knife4j
  • 功能介绍

二、注解说明

  • @Api()用于类;
    表示标识这个类是swagger的资源
  • @ApiOperation()用于方法;
    表示一个http请求的操作
  • @ApiParam()用于方法,参数,字段说明;
    表示对参数的添加元数据(说明或是否必填等)
  • @ApiModel()用于类
    表示对类进行说明,用于参数用实体类接收
  • @ApiModelProperty()用于方法,字段
    表示对model属性的说明或者数据操作更改
  • @ApiIgnore()用于类,方法,方法参数
    表示这个方法或者类被忽略
  • @ApiImplicitParam() 用于方法
    表示单独的请求参数
  • @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

三、注解使用

@Api()
用于类;表示标识这个类是swagger的资源
tags–表示说明
value–也是说明,可以使用tags替代
但是tags如果有多个值,会生成多个list

@Api(value="用户controller",tags={"用户操作接口"})@RestControllerpublic class UserController {}

效果如下图

@ApiOperation()
用于方法;表示一个http请求的操作
value用于方法描述
notes用于提示内容
tags可以重新分组(视情况而用)


@ApiParam()
用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
name–参数名
value–参数说明
required–是否必填

@Api(value="用户controller",tags={"用户操作接口"})@RestControllerpublic class UserController {       @ApiOperation(value="获取用户信息",tags={"获取用户信息copy"},notes="注意问题点")       @GetMapping("/getUserInfo")       public User getUserInfo(@ApiParam(name="id",value="用户id",required=true) Long id,@ApiParam(name="username",value="用户名") String username) {         // userService可忽略,是业务逻辑          User user = userService.getUserInfo();          return user;   }}

效果如下图

@ApiModel()
用于类,表示对类进行说明,用于参数用实体类接收
value–表示对象名
description–描述都可省略


@ApiModelProperty()
用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏

@ApiModel(value="user对象",description="用户对象user")public class User implements Serializable{      private static final long serialVersionUID = 1L;     @ApiModelProperty(value="用户名",name="username",example="xingguo")     private String username;     @ApiModelProperty(value="状态",name="state",required=true)      private Integer state;  @ApiModelProperty(value="id数组",hidden=true)      private String[] ids; 
@ApiOperation("更改用户信息")  @PostMapping("/updateUserInfo")  public int updateUserInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){       int num = userService.updateUserInfo(user);       return num;  }

效果如下图

@ApiIgnore()
用于类或者方法上,可以不被swagger显示在页面上
比较简单, 这里不做举例


@ApiImplicitParam()
用于方法
表示单独的请求参数


@ApiImplicitParams()
用于方法,包含多个 @ApiImplicitParam
name–参数ming
value–参数说明
dataType–数据类型
paramType–参数类型
example–举例说明

@ApiOperation("查询测试")  @GetMapping("select")  //@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")  @ApiImplicitParams({    @ApiImplicitParam(name="name",value="用户名",dataType="string", paramType = "query",example="xingguo"),  @ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query")})  public void select(){  }

总结:到这里Swagger初步使用就介绍完了,但是原生页面看上去有点丑陋,后续会介绍Spring Boot整合Knife4j优美的界面。

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章