获取springboot所有接口

本文最后更新于:2025年2月18日 下午

获取springboot所有接口

EndpointScanner.java

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
* 扫描所有可用接口
* @author peter
* @date 2024/11/25
* @className EndpointScanner
*/
@Component
public class EndpointScanner {

@Resource
private ApplicationContext applicationContext;


/**
* 扫描所有接口,处理后存到文件中
* @author peter
* @date 2024/11/30
* @className EndpointScanner
*/
public void scanAndWriteEndpointsToFile(String filePath) throws IOException {
RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);

// 获取所有映射的路径
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
StringBuilder endpoints = new StringBuilder();

// 遍历所有的映射关系,获取每个接口的路径
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
RequestMappingInfo mappingInfo = entry.getKey();
HandlerMethod handlerMethod = entry.getValue();

Set<String> patterns = mappingInfo.getPatternsCondition().getPatterns();
// 获取请求方法
Set<RequestMethod> methods = mappingInfo.getMethodsCondition().getMethods();

// 获取控制器类名和处理方法名
String controllerClass = handlerMethod.getBeanType().getName();
String methodName = handlerMethod.getMethod().getName();

for (String pattern : patterns) {
// 过滤掉路径参数{id}
if (pattern.contains("{")){
String substring = pattern.substring(0, pattern.indexOf("{"));
if (methods.isEmpty()) {
// 如果未指定方法,默认为支持所有方法
endpoints.append("ANY ").append(substring).append(" -> ").append(controllerClass)
.append("#").append(methodName)
.append("\n");
} else {
for (RequestMethod method : methods) {
endpoints.append(method.name()).append(" ").append(substring).append(" -> ").append(controllerClass)
.append("#").append(methodName)
.append("\n");
}
}
}else {
if (methods.isEmpty()) {
// 如果未指定方法,默认为支持所有方法
endpoints.append("ANY ").append(pattern).append(" -> ").append(controllerClass)
.append("#").append(methodName)
.append("\n");
} else {
for (RequestMethod method : methods) {
endpoints.append(method.name()).append(" ").append(pattern).append(" -> ").append(controllerClass)
.append("#").append(methodName)
.append("\n");
}
}
}


}
}

// 将接口路径写入文件
File file = new File(filePath);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(endpoints.toString());
}
}
}

调用接口

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
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;

/**
* 将可用接口写入可访问接口列表文件
* @author peter
* @date 2024/11/25
* @className EndpointLoader
* @description TODO
*/
@Component
public class EndpointLoader {

@Autowired
private EndpointScanner endpointScanner;

@PostConstruct
public void init() throws IOException {
String filePath = System.getProperty("user.dir") + "/secret/access_api_list.txt"; // 文件保存路径
endpointScanner.scanAndWriteEndpointsToFile(filePath);
}
}

输出示例:

假设接口:

1
2
3
4
5
6
7
8
9
@GetMapping("/api/users")
public String getUsers() { ... }

@PostMapping("/api/users")
public String createUser() { ... }

@RequestMapping("/api/products")
public String handleProducts() { ... }

文件存储:【请求类型】 【请求路径】 -> 【所在类】【方法名】

1
2
3
GET /api/users -> com.example.controller.UserController#getUsers
POST /api/users -> com.example.controller.UserController#createUser
ANY /api/products -> com.example.controller.ProductController#handleProducts

可以视情况选择

例如只获取请求方法类型和请求路径

EndpointScanner.java

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
* 扫描所有可用接口
* @author peter
* @date 2024/11/25
* @className EndpointScanner
*/
@Component
public class EndpointScanner {

@Resource
private ApplicationContext applicationContext;


/**
* 扫描所有接口,处理后存到文件中
* @author peter
* @date 2024/11/30
* @className EndpointScanner
*/
public void scanAndWriteEndpointsToFile(String filePath) throws IOException {
RequestMappingHandlerMapping handlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);

// 获取所有映射的路径
Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
StringBuilder endpoints = new StringBuilder();

// 遍历所有的映射关系,获取每个接口的路径
for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
RequestMappingInfo mappingInfo = entry.getKey();

Set<String> patterns = mappingInfo.getPatternsCondition().getPatterns();
// 获取请求方法
Set<RequestMethod> methods = mappingInfo.getMethodsCondition().getMethods();

for (String pattern : patterns) {
if (pattern.contains("{")){
String substring = pattern.substring(0, pattern.indexOf("{"));
if (methods.isEmpty()) {
// 如果未指定方法,默认为支持所有方法
endpoints.append("ANY ").append(substring).append("\n");
} else {
for (RequestMethod method : methods) {
endpoints.append(method.name()).append(" ").append(substring).append("\n");
}
}
}else {
if (methods.isEmpty()) {
// 如果未指定方法,默认为支持所有方法
endpoints.append("ANY ").append(pattern).append("\n");
} else {
for (RequestMethod method : methods) {
endpoints.append(method.name()).append(" ").append(pattern).append("\n");
}
}
}
}
}

// 将接口路径写入文件
File file = new File(filePath);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(endpoints.toString());
}
}
}


获取springboot所有接口
https://superlovelace.top/2024/11/30/获取项目所有接口/
作者
棱境
发布于
2024年11月30日
更新于
2025年2月18日
许可协议