This commit is contained in:
牛子源 2025-05-27 11:46:57 +08:00
parent 02fb28eee0
commit 813e51bf9d
7 changed files with 147 additions and 12 deletions

View File

@ -0,0 +1,135 @@
package com.syjiaer.clinic.server;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.ApiOperation;
import lombok.*;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
public class BulidApi {
public static void main(String[] args) {
List<ApiInfo> apiPaths = scanControllersWithDocs("com.syjiaer.clinic.server");
Map<String, Object> nestedJson = convertToNestedJson(apiPaths);
String jsonContent = JSON.toJSONString(nestedJson);
try (FileWriter fileWriter = new FileWriter("D:\\api.json")) {
fileWriter.write(jsonContent);
System.out.println("成功写入 D:\\api.json");
} catch (IOException e) {
throw new RuntimeException("文件写入失败", e);
}
}
public static Map<String, Object> convertToNestedJson(List<ApiInfo> apiPaths) {
Map<String, Object> root = new LinkedHashMap<>();
for (ApiInfo apiInfo : apiPaths) {
String path = apiInfo.getPath();
// 去除开头和结尾的斜杠并按斜杠分割
String[] parts = path.replaceAll("^/|/$", "").split("/");
Map<String, Object> currentLevel = root;
// 处理路径的每一部分(除最后一部分)
for (int i = 0; i < parts.length - 1; i++) {
String part = parts[i];
// 如果当前节点不是Map说明是叶子节点需要转换为Map
if (!(currentLevel.get(part) instanceof Map)) {
// 如果已有数据是ApiInfo先保存到临时变量
Object existingValue = currentLevel.get(part);
currentLevel.put(part, new LinkedHashMap<String, Object>());
if (existingValue != null) {
// 将原有值放到特殊键下"_data"
((Map<String, Object>) currentLevel.get(part)).put("_data", existingValue);
}
}
currentLevel = (Map<String, Object>) currentLevel.get(part);
}
// 最后一部分作为叶子节点
String lastPart = parts[parts.length - 1];
// 检查是否已经存在路径节点
if (currentLevel.containsKey(lastPart) && currentLevel.get(lastPart) instanceof Map) {
// 如果已经存在Map节点将API信息放入特殊键
((Map<String, Object>) currentLevel.get(lastPart)).put("_data", apiInfo);
} else {
currentLevel.put(lastPart, apiInfo);
}
}
return root;
}
public static List<ApiInfo> scanControllersWithDocs(String basePackage) {
List<ApiInfo> apiInfos = new ArrayList<>();
ClassPathScanningCandidateComponentProvider scanner =
new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
scanner.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
Set<BeanDefinition> beans = scanner.findCandidateComponents(basePackage);
try {
for (BeanDefinition bean : beans) {
Class<?> clazz = Class.forName(bean.getBeanClassName());
// 获取类注解
RequestMapping classMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class);
String classPath = classMapping != null && classMapping.value().length > 0 ?
classMapping.value()[0] : "";
// 扫描方法
for (Method method : clazz.getMethods()) {
RequestMapping methodMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (methodMapping != null) {
String methodPath = methodMapping.value().length > 0 ?
methodMapping.value()[0] : "";
String fullPath = (classPath + "/" + methodPath).replaceAll("/+", "/");
// 获取方法注释
String comment = getMethodComment(method);
apiInfos.add(new ApiInfo(fullPath, comment));
}
}
}
} catch (Exception e) {
throw new RuntimeException("Controller扫描失败", e);
}
return apiInfos;
}
// 新增方法注释解析方法
private static String getMethodComment(Method method) {
// 1. 优先获取Swagger的ApiOperation注解
ApiOperation apiOperation = AnnotationUtils.findAnnotation(method, ApiOperation.class);
if (apiOperation != null) {
return apiOperation.value();
}
// 2. 次选JavaDoc注释需额外实现
return ""; // 暂留空
}
@Data
@NoArgsConstructor
@AllArgsConstructor
// 新增返回数据结构
public static class ApiInfo {
private String path;
private String comment;
// 构造方法/getter/setter省略
}
}

View File

@ -87,8 +87,8 @@ public class ItemController extends BaseController {
* 删除项目
*/
@ApiOperation("删除项目")
@RequestMapping("/delete")
public Result<?> deleteItem() {
@RequestMapping("/del")
public Result<?> del() {
Integer id = parmsUtil.getInteger("id");
itemService.delete(id);
return success();

View File

@ -45,9 +45,9 @@ public class OrganizationMemberController extends BaseController {
}
@ApiOperation("删除成员")
@RequestMapping("/delete")
@RequestMapping("/del")
@RecordCommonLog(operation = "删除成员")
public Result<?> delete() {
public Result<?> del() {
Integer id = parmsUtil.getInteger("id");
if(id == null){
throw new MessageException("id参数为空");

View File

@ -60,8 +60,8 @@ public class OrganizationSectionController extends BaseController {
@ApiOperation("删除科室")
@RecordCommonLog(operation = "删除科室")
@RequestMapping("/delete")
public Result<?> delete() {
@RequestMapping("/del")
public Result<?> del() {
Integer id = parmsUtil.getInteger("id");
if(id == null){
throw new MessageException("id参数为空");

View File

@ -50,9 +50,9 @@ public class PatientController extends BaseController {
@ApiOperation("删除患者")
@RequestMapping("/delete")
@RequestMapping("/del")
@RecordCommonLog(operation = "删除患者")
public Result<Integer> delete() {
public Result<Integer> del() {
int patientId = parmsUtil.getInteger("id", "ID不能为空");
return success(patientInfoService.removeById(patientId));
}

View File

@ -60,8 +60,8 @@ public class PatientLevelConfigController extends BaseController {
@ApiOperation("删除一个会员等级配置")
@RecordCommonLog(operation = "删除一个会员等级配置")
@RequestMapping("/delete")
public Result<Object> delete() {
@RequestMapping("/del")
public Result<Object> del() {
int id = parmsUtil.getInteger("id", "ID不能为空");
vipLevelConfigService.delete(id);

View File

@ -93,8 +93,8 @@ public class RegistrationController extends BaseController {
*/
@ApiOperation("删除挂号信息")
@RecordCommonLog(operation = "删除挂号信息")
@RequestMapping("/delete")
public Result<?> delete() {
@RequestMapping("/del")
public Result<?> del() {
Integer id = parmsUtil.getInteger("id");
if (id == null) {
throw new MessageException("id参数为空");