这种代码里面的Controller和里面的方法怎么获取
代码:
java
/**
* 获取所有Controller名称
*/
@ApiDescription("获取所有Controller名称")
@PostMapping("/getControllerNames")
public Result getControllerNames() {
return dataDesensitizationRulesService.getAllControllerNames();
}
/**
* 根据Controller获取方法列表
*/
@ApiDescription("根据Controller获取方法列表")
@PostMapping("/getControllerMethods")
public Result getControllerMethods(@RequestBody @Valid DataDesensitizationRulesMethodsForm dataDesensitizationRulesMethodsForm) {
return dataDesensitizationRulesService.getControllerMethods(dataDesensitizationRulesMethodsForm);
}
java
/**
* 获取所有Controller名称
*/
public Result getAllControllerNames() {
List<String> controllerNames = new ArrayList<>();
// 获取所有带有 @RestController 注解的Bean
Map<String, Object> restControllers = applicationContext.getBeansWithAnnotation(RestController.class);
restControllers.values().forEach(bean -> {
Class<?> clazz = AopUtils.getTargetClass(bean); // 获取原始类
controllerNames.add(clazz.getSimpleName()); // 获取简单类名
});
// 获取所有带有 @Controller 注解的Bean
Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(Controller.class);
controllers.values().forEach(bean -> {
Class<?> clazz = AopUtils.getTargetClass(bean); // 获取原始类
controllerNames.add(clazz.getSimpleName()); // 获取简单类名
});
return Result.ok().build("allControllerNames", controllerNames);
}
/**
* 根据Controller名称获取方法列表
*/
public Result getControllerMethods(DataDesensitizationRulesMethodsForm dataDesensitizationRulesMethodsForm) {
String controllerName = dataDesensitizationRulesMethodsForm.getControllerName();
try {
List<Map<String, Object>> controllerDetails = getControllerDetails();
Optional<Map<String, Object>> controllerOpt = controllerDetails.stream()
.filter(info -> controllerName.equals(info.get("className")))
.findFirst();
if (controllerOpt.isPresent()) {
@SuppressWarnings("unchecked")
List<Map<String, String>> methods = (List<Map<String, String>>) controllerOpt.get().get("methods");
return Result.ok().build("allControllerMethods",methods);
} else {
return Result.error("Controller不存在");
}
} catch (Exception e) {
return Result.error("获取失败: " + e.getMessage());
}
}
/**
* 获取Controller详细信息
*/
public List<Map<String, Object>> getControllerDetails() {
List<Map<String, Object>> controllerDetails = new ArrayList<>();
// 处理@RestController
Map<String, Object> restControllers = applicationContext.getBeansWithAnnotation(RestController.class);
processControllers(restControllers, controllerDetails);
// 处理@Controller
Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(Controller.class);
processControllers(controllers, controllerDetails);
return controllerDetails;
}
private void processControllers(Map<String, Object> controllers, List<Map<String, Object>> details) {
for (Map.Entry<String, Object> entry : controllers.entrySet()) {
Object controller = entry.getValue();
Class<?> clazz = AopUtils.getTargetClass(controller); // 关键修改:获取原始类
Map<String, Object> info = new HashMap<>();
info.put("beanName", entry.getKey());
info.put("className", clazz.getSimpleName());
info.put("fullClassName", clazz.getName());
// 获取RequestMapping注解信息(从原始类上获取)
RequestMapping classMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class);
if (classMapping != null) {
info.put("basePath", Arrays.toString(classMapping.value()));
}
// 获取方法信息(从原始类上获取)
List<Map<String, String>> methods = new ArrayList<>();
for (Method method : clazz.getDeclaredMethods()) {
// 使用AnnotationUtils查找注解,确保能从接口/父类方法上找到注解
GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class);
PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class);
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (getMapping != null || postMapping != null || requestMapping != null) {
Map<String, String> methodInfo = new HashMap<>();
methodInfo.put("methodName", method.getName());
if (getMapping != null) {
methodInfo.put("httpMethod", "GET");
methodInfo.put("path", Arrays.toString(getMapping.value()));
} else if (postMapping != null) {
methodInfo.put("httpMethod", "POST");
methodInfo.put("path", Arrays.toString(postMapping.value()));
} else if (requestMapping != null) {
methodInfo.put("httpMethod", Arrays.toString(requestMapping.method()));
methodInfo.put("path", Arrays.toString(requestMapping.value()));
}
methods.add(methodInfo);
}
}
info.put("methods", methods);
details.add(info);
}
}
效果图: