1. 概述
基于Python代码编程,统计Java项目中所有接口的数量,输入为项目路径,输出接口数量,只统计restfull风格的接口。
2.代码实现
import os
import re
def find_restful_endpoints(project_dir):
restful_pattern = re.compile(r'@(?:GetMapping|PostMapping|PutMapping|DeleteMapping|PatchMapping)\s*\(\s*["\']?([^"\']+)["\']?\)')
endpoints = []
for root, dirs, files in os.walk(project_dir):
for file in files:
if file.endswith('.java'):
file_path = os.path.join(root, file)
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
endpoints.extend(restful_pattern.findall(content))
return endpoints
if __name__ == "__main__":
project_dir = "path/to/your/java/project"
restful_endpoints = find_restful_endpoints(project_dir)
print("接口数量:", len(restful_endpoints))