JAVA自带的常用注解了解一下:
https://www.runoob.com/w3cnote/java-annotation.html
说一下思路:
1.扫描包
2.再找到所有的Class文件,带有@Controller注解的Class文件
3.再找到所有的带有@PostMapping的方法
注意:如果你想要到拿到@GetMapping/@PutMapping/@DeleteMapping/@PatchMapping的URL的话需要注意,这四种的URL如果采用标准的RESTFUL风格,是动态的URL,所以建议使写一个自定义的注解来表明这个Method的URL。
import cn.hutool.core.util.ClassUtil;
public static void main(String[] args) {
Set<Class<?>> classes = ClassUtil.scanPackage("com.alibaba.finance.smms");
for (Class aclass : classes) {
Controller aclassController = AnnotationUtil.getAnnotation(aclass, Controller.class);
RequestMapping interfaceAnnotation = AnnotationUtil.getAnnotation(aclass, RequestMapping.class);
//如果接口没有带有@Controller/@RestController或@RequestMapping,则返回空
if (null == aclassController || null == interfaceAnnotation) {
continue;
}
Method[] methods = aclass.getMethods();
for (Method method : methods) {
if (!checkUrlExist(method)) {
continue;
}
String url = interfaceAnnotation.value()[0] + method.getAnnotation(PostMapping.class).value()[0];
System.out.println("url=" + url);
}
}
}
/**
* 检查方法
* 判断几种请求方式是否存在URL
*
* @param method
* @return
*/
private static boolean checkUrlExist(Method method) {
PostMapping postMapping = method.getAnnotation(PostMapping.class);
if ( null == postMapping) {
return false;
}
return true;
}