@Autowired
private WebApplicationContext applicationContext;
@ApiOperation(value = "找接口路径", notes = "找接口路径")
@PostMapping("/api/common/getInterfacePath")
public void getInterfacePath(HttpServletResponse response){
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
// 获取url与类和方法的对应信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
Map<String, String> map1 = new HashMap<String, String>();
RequestMappingInfo info = m.getKey();
HandlerMethod method = m.getValue();
//获取当前方法所在类名
// Class<?> bean = method.getBeanType();
//获取方法上注解以及注解值
ApiOperation methodAnnotation = method.getMethodAnnotation(ApiOperation.class);
PatternsRequestCondition p = info.getPatternsCondition();
for (String url : p.getPatterns()) {
if (url.length()>11){
String substring = url.substring(0, 11);
if (substring.equals("/api/common")){
//接口名
map1.put("url", url);
//methodAnnotation.value() swagger注释value
map1.put("value",ObjectUtils.isNotEmpty(methodAnnotation)? methodAnnotation.value() : null);
list.add(map1);
}
}
/* map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
map1.put("method", method.getMethod().getName()); // 方法名*/
}
}
try {
ExcelData data = new ExcelData();
List<List<Object>> rows = new ArrayList();
data.setName("一县一品-乡村振兴裸奔接口导出");//sheet表名称
//表头
List<String> titles = new ArrayList();
titles.add("接口地址");
titles.add("接口名称");
data.setTitles(titles);
for (Map<String,String> map1 : list) {
List<Object> row1 = new ArrayList();
row1.add(map1.get("url"));
row1.add(map1.get("value"));
rows.add(row1);
}
data.setRows(rows);
ExportExcelUtils.exportExcel(response, "乡村振兴裸奔接口导出.xlsx" + new Date(), data);
}catch (Exception e){
e.printStackTrace();
}
}