java接入logit_java反射注解妙用-获取所有接口说明

packagecom.wwj.springboot;importcom.alibaba.fastjson.JSON;importcom.wwj.springboot.model.Auth;importio.swagger.annotations.Api;importio.swagger.annotations.ApiOperation;importorg.apache.shiro.authz.annotation.RequiresPermissions;importorg.reflections.Reflections;importorg.reflections.scanners.MethodAnnotationsScanner;importorg.reflections.util.ClasspathHelper;importorg.reflections.util.ConfigurationBuilder;import org.springframework.web.bind.annotation.*;importjava.lang.reflect.Method;importjava.util.ArrayList;importjava.util.Date;importjava.util.List;importjava.util.Set;public classAnnoTest {public static voidmain(String[] args) {

getRequestMappingMethod("com.wwj.springboot.controller");

}/***@paramscanPackage 需要扫描的包路径*/

private static voidgetRequestMappingMethod(String scanPackage) {//设置扫描路径

Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(scanPackage)).setScanners(newMethodAnnotationsScanner()));//扫描包内带有@RequiresPermissions注解的所有方法集合

Set methods = reflections.getMethodsAnnotatedWith(RequiresPermissions.class);

List list = new ArrayList<>();

Date now= newDate();//循环获取方法

methods.forEach(method ->{//用于保存方法的请求类型

String methodType = "";//获取类上的@RequestMapping注解的值,作为请求的基础路径

String authUrl = method.getDeclaringClass().getAnnotation(RequestMapping.class).value()[0];//获取方法上的@PutMapping,@GetMapping,@PostMapping,@DeleteMapping注解的值,作为请求路径,并区分请求方式

if (method.getAnnotation(PutMapping.class) != null) {

methodType= "put";if (method.getAnnotation(PutMapping.class).value().length > 0) {

authUrl= method.getAnnotation(PutMapping.class).value()[0];

}

}else if (method.getAnnotation(GetMapping.class) != null) {

methodType= "get";if (method.getAnnotation(GetMapping.class).value().length > 0) {

authUrl= method.getAnnotation(GetMapping.class).value()[0];

}

}else if (method.getAnnotation(PostMapping.class) != null) {

methodType= "post";if (method.getAnnotation(PostMapping.class).value().length > 0) {

authUrl= method.getAnnotation(PostMapping.class).value()[0];

}

}else if (method.getAnnotation(DeleteMapping.class) != null) {if (method.getAnnotation(DeleteMapping.class).value().length > 0) {

authUrl= method.getAnnotation(DeleteMapping.class).value()[0];

}

}//使用Auth对象来保存值

Auth auth = newAuth();

auth.setMethodType(methodType);

auth.setAuthUniqueMark(method.getAnnotation(RequiresPermissions.class).value()[0]);

auth.setAuthUrl(authUrl);

auth.setAuthName(method.getDeclaringClass().getAnnotation(Api.class).value() + "-" + method.getAnnotation(ApiOperation.class).value());

auth.setCreateTime(now);

list.add(auth);

});//TODO 输出到控制台,此处存数据库即可System.out.println(JSON.toJSONString(list));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值