以下是 Java 代码示例:

import java.io.FileWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import com.google.gson.Gson;

public class ControllerScanner { public static void main(String[] args) throws Exception { String projectPath = 'path/to/your/project'; List controllerPaths = scanControllerFiles(projectPath); List controllerInfoList = scanControllerMethods(controllerPaths); String json = new Gson().toJson(controllerInfoList); writeToFile(json, 'path/to/your/output/file'); }

private static List<String> scanControllerFiles(String projectPath) throws Exception {
    List<String> controllerPaths = new ArrayList<>();
    Files.walk(Paths.get(projectPath)).forEach(filePath -> {
        if (Files.isRegularFile(filePath) && filePath.toString().endsWith('Controller.java')) {
            controllerPaths.add(filePath.toString());
        }
    });
    return controllerPaths;
}

private static List<ControllerInfo> scanControllerMethods(List<String> controllerPaths) throws Exception {
    List<ControllerInfo> controllerInfoList = new ArrayList<>();
    for (String controllerPath : controllerPaths) {
        String controllerPackage = getControllerPackage(controllerPath);
        Class<?> controllerClass = Class.forName(controllerPackage);
        if (controllerClass.isAnnotationPresent(Controller.class)) {
            Method[] controllerMethods = controllerClass.getDeclaredMethods();
            for (Method controllerMethod : controllerMethods) {
                if (controllerMethod.isAnnotationPresent(GetMapping.class) || 
                        controllerMethod.isAnnotationPresent(PostMapping.class)) {
                    String functionName = controllerMethod.getName();
                    String address = getAddress(controllerMethod);
                    ControllerInfo controllerInfo = new ControllerInfo(functionName, address);
                    controllerInfoList.add(controllerInfo);
                }
            }
        }
    }
    return controllerInfoList;
}

private static String getControllerPackage(String controllerPath) {
    String packagePath = controllerPath.replace('/', '.');
    packagePath = packagePath.substring(0, packagePath.lastIndexOf('.java'));
    return packagePath;
}

private static String getAddress(Method controllerMethod) {
    Annotation[] annotations = controllerMethod.getDeclaredAnnotations();
    for (Annotation annotation : annotations) {
        if (annotation instanceof GetMapping) {
            GetMapping getMapping = (GetMapping) annotation;
            return getMapping.value()[0];
        } else if (annotation instanceof PostMapping) {
            PostMapping postMapping = (PostMapping) annotation;
            return postMapping.value()[0];
        }
    }
    return '';
}

private static void writeToFile(String json, String filePath) throws Exception {
    FileWriter fileWriter = new FileWriter(filePath);
    fileWriter.write(json);
    fileWriter.close();
}

}

class ControllerInfo { private String functionName; private String address;

public ControllerInfo(String functionName, String address) {
    this.functionName = functionName;
    this.address = address;
}

public String getFunctionName() {
    return functionName;
}

public void setFunctionName(String functionName) {
    this.functionName = functionName;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

}

说明:

  • 代码使用了 Java 8 的 Path 和 Files 类来扫描项目中的 Controller 文件。
  • 代码使用了 Java 反射机制来获取 Controller 类和方法的信息。
  • 代码使用了 Google Gson 库来将 Controller 信息转化为 JSON 格式。
  • 代码将 JSON 格式的信息写入到指定的输出文件中。
  • 代码中的 ControllerInfo 类用于存储 Controller 方法的信息。
Java 项目 Controller 方法扫描和 JSON 输出

原文地址: https://www.cveoy.top/t/topic/lC9J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录