添加ApiOperation

This commit is contained in:
LiJianZhao 2025-05-27 16:20:10 +08:00
parent 813e51bf9d
commit 24de7433b8
3 changed files with 64 additions and 7 deletions

View File

@ -16,19 +16,75 @@ import java.lang.reflect.Method;
import java.util.*; import java.util.*;
public class BulidApi { public class BulidApi {
public static void main(String[] args) { public static void main(String[] args) {
List<ApiInfo> apiPaths = scanControllersWithDocs("com.syjiaer.clinic.server"); List<ApiInfo> apiPaths = scanControllersWithDocs("com.syjiaer.clinic.server");
Map<String, Object> nestedJson = convertToNestedJson(apiPaths); Map<String, Object> nestedJson = convertToNestedJson(apiPaths);
String jsonContent = JSON.toJSONString(nestedJson); generateAPITsFile(apiPaths, "D:\\API.ts");
try (FileWriter fileWriter = new FileWriter("D:\\api.json")) { }
fileWriter.write(jsonContent);
System.out.println("成功写入 D:\\api.json"); private static String cleanPath(String originalPath) {
// 1. 移除路径参数
String pathWithoutParams = originalPath.replaceAll("/\\{[^/]+}", "");
// 2. 分割路径并只保留前三级
String[] parts = pathWithoutParams.replaceAll("^/|/$", "").split("/");
if (parts.length > 3) {
return String.join("/", Arrays.copyOfRange(parts, 0, 3));
}
return pathWithoutParams;
}
private static void generateNamespace(StringBuilder builder, Map<String, Object> map, int indentLevel) {
String indent = " ".repeat(indentLevel);
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Map) {
// 处理嵌套命名空间
builder.append(indent).append("export namespace ").append(capitalize(key)).append(" {\n");
generateNamespace(builder, (Map<String, Object>) value, indentLevel + 1);
builder.append(indent).append("}\n\n");
} else if (value instanceof ApiInfo) {
// 处理API端点
ApiInfo apiInfo = (ApiInfo) value;
String path = apiInfo.getPath().replaceAll("^/|/$", "");
String comment = apiInfo.getComment().isEmpty() ? "" : "//" + apiInfo.getComment();
builder.append(indent).append("export const ")
.append(capitalize(key)).append(" = '")
.append(path).append("'")
.append(comment).append(";\n");
}
}
}
private static String capitalize(String str) {
if (str == null || str.isEmpty()) {
return str;
}
return str.substring(0, 1).toUpperCase() + str.substring(1);
}
public static void generateAPITsFile(List<ApiInfo> apiPaths, String outputPath) {
Map<String, Object> nestedJson = convertToNestedJson(apiPaths);
StringBuilder tsBuilder = new StringBuilder();
tsBuilder.append("export namespace API {\n");
generateNamespace(tsBuilder, nestedJson, 1);
tsBuilder.append("}\n");
try (FileWriter fileWriter = new FileWriter(outputPath)) {
fileWriter.write(tsBuilder.toString());
System.out.println("成功写入 " + outputPath);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("文件写入失败", e); throw new RuntimeException("文件写入失败", e);
} }
} }
public static Map<String, Object> convertToNestedJson(List<ApiInfo> apiPaths) { public static Map<String, Object> convertToNestedJson(List<ApiInfo> apiPaths) {
Map<String, Object> root = new LinkedHashMap<>(); Map<String, Object> root = new LinkedHashMap<>();
@ -94,6 +150,7 @@ public class BulidApi {
String methodPath = methodMapping.value().length > 0 ? String methodPath = methodMapping.value().length > 0 ?
methodMapping.value()[0] : ""; methodMapping.value()[0] : "";
String fullPath = (classPath + "/" + methodPath).replaceAll("/+", "/"); String fullPath = (classPath + "/" + methodPath).replaceAll("/+", "/");
fullPath = cleanPath(fullPath);
// 获取方法注释 // 获取方法注释
String comment = getMethodComment(method); String comment = getMethodComment(method);

View File

@ -18,7 +18,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/item") @RequestMapping("/item/base")
public class ItemController extends BaseController { public class ItemController extends BaseController {
@Autowired @Autowired

View File

@ -18,7 +18,7 @@ import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@RequestMapping("/statistics") @RequestMapping("/statistics/base")
@RestController @RestController
public class StatisticsController extends BaseController { public class StatisticsController extends BaseController {