引入依赖

1
2
3
4
5
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

添加注解

在启动类添加@EnableOpenApi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ledao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;

/**
* @author LeDao
*/
@EnableOpenApi
@SpringBootApplication
public class SwaggerTestApplication {

public static void main(String[] args) {
SpringApplication.run(SwaggerTestApplication.class, args);
}

}

在Controller类上添加@Api(tags = “该Controller类的用途”),参数tags主要是描述该Controller类的用途,在Controller类的方法上添加@ApiOperation(value = “该方法的用途”),参数value主要是描述该方法的用途

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.ledao.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* 首页Controller
*
* @author LeDao
* @company
* @create 2021-10-05 17:35
*/
@Api(tags = "首页Controller")
@Controller
public class IndexController {

@ApiOperation(value = "跳转到首页")
@ResponseBody
@GetMapping("/")
public String root() {
return "<a href='http://localhost:8080/student/delete?id=2'>删除学生</a>";
}
}

结果

浏览器地址栏输入:http://localhost:8080/swagger-ui/

可以看到项目中拥有的Controller类以及该类的方法

点击具体方法可以看到该方法返回的类型以及参数等

PS.

Swagger3常用注解查看博客:Swagger3常用注解 | LeDao的博客 (zoutl.cn)