3 Swagger3 常用配置注解讲解 结合SpringBoot2

x33g5p2x  于2021-09-26 转载在 Spring  
字(3.2k)|赞(0)|评价(0)|浏览(379)

3.1 Swagger3常用配置如下:

swagger提供了一些配置用来描述接口,下面是一些常用的注解,必须掌握;

@Api:用在请求的类上,表示对类的说明
    tags="说明该类的作用,可以在UI界面上看到的注解"
    value="该参数没什么意义,在UI界面上也看到,所以不需要配置"

@ApiOperation:用在请求的方法上,说明方法的用途、作用
    value="说明方法的用途、作用"
    notes="方法的备注说明"

@ApiImplicitParams:用在请求的方法上,表示一组参数说明
    @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
        name:参数名
        value:参数的汉字说明、解释
        required:参数是否必须传
        paramType:参数放在哪个地方
            · header --> 请求参数的获取:@RequestHeader
            · query --> 请求参数的获取:@RequestParam
            · path(用于restful接口)--> 请求参数的获取:@PathVariable
            · div(不常用)
            · form(不常用)    
        dataType:参数类型,默认String,其它值dataType="Integer"       
        defaultValue:参数的默认值

@ApiResponses:用在请求的方法上,表示一组响应
    @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
        code:数字,例如400
        message:信息,例如"请求参数没填好"
        response:抛出异常的类

@ApiModel:用于响应类上,表示一个返回响应数据的信息
            (这种一般用在post创建的时候,使用@RequestBody这样的场景,
            请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    @ApiModelProperty:用在属性上,描述响应类的属性

3.2 /*/*实例一 @ApiImplicitParams@ApiImplicitParam 参数描述

post方式,根据name和age两个参数查询数据,返回信息;

我们用@ApiImplicitParams@ApiImplicitParam,描述请求参数

/** * 查询 * @param name * @param age * @return */
@PostMapping("/search")
@ApiImplicitParams({
		@ApiImplicitParam(name = "name",value = "姓名",required = true,paramType = "query"),
		@ApiImplicitParam(name = "age",value = "年龄",required = true,paramType = "query",dataType = "Integer")
})
@ApiOperation("测试查询")
public String search(String name,Integer age){
	return name+":"+age;
}

swagger控制台显示:

3.3 /*/实例二 @ApiModel,@ApiModelProperty//*实体参数描述

我们搞一个用户信息添加,使用@ApiModel,@ApiModelProperty注解来描述输入参数;

先搞一个用户信息实体User.java

package com.java1234.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/** * 用户实体 * @author java1234_小锋 * @site www.java1234.com * @company 南通小锋网络科技有限公司 * @create 2021-09-26 9:10 */
@ApiModel("用户信息实体")
public class User {

    @ApiModelProperty("编号")
    private Integer id;

    @ApiModelProperty("姓名")
    private String name;

    @ApiModelProperty("年龄")
    private Integer age;

    public User() {
    }

    public User(Integer id,String name, Integer age) {
        this.id=id;
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

参数上,直接用User user

/** * 添加测试 * @param user * @return */
@PostMapping("/add")
@ApiOperation("测试添加")
public String add(User user){
	return user.getName()+":"+user.getAge();
}

swagger控制台显示:

3.4 实例三 @ApiResponses@ApiResponse

我们搞一个根据id获取用户信息案例,通过@PathVariable获取id,返回User对象,以及通过@ApiResponses@ApiResponse,描述响应码对应的描述信息

@GetMapping("/user/{id}")
@ApiOperation("根据ID获取用户信息")
@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "用户编号",required = true,paramType = "path")
})
@ApiResponses({
		@ApiResponse(code=408,message="指定业务得报错信息,返回客户端"),
		@ApiResponse(code=400,message="请求参数没填好"),
		@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
})
public User load(@PathVariable("id") Integer id){
	return new User(id,"jack",32);
}

swagger控制台显示:

Schemas也对应有视图用户实体描述信息显示:

相关文章