接口文档生成工具
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package org.nl.config;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.poi.excel.BigExcelWriter;
|
||||
import cn.hutool.poi.excel.ExcelUtil;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.apache.poi.xssf.streaming.SXSSFSheet;
|
||||
import org.nl.common.anno.Log;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import springfox.documentation.RequestHandler;
|
||||
import springfox.documentation.spi.service.RequestHandlerProvider;
|
||||
import springfox.documentation.spring.web.WebMvcRequestHandler;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
/*
|
||||
* @author ZZQ
|
||||
* @Date 2023/10/17 14:51
|
||||
* 接口文档工具
|
||||
*/
|
||||
//@Component
|
||||
public class ApiDocScan implements SmartLifecycle {
|
||||
private final List<RequestHandlerProvider> handlerProviders;
|
||||
|
||||
@Autowired
|
||||
public ApiDocScan(List<RequestHandlerProvider> handlerProviders) {
|
||||
this.handlerProviders = handlerProviders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
RequestHandlerProvider requestHandlerProvider = handlerProviders.get(0);
|
||||
List<RequestHandler> requestHandlers = requestHandlerProvider.requestHandlers();
|
||||
List<Map> urlMapping = new ArrayList<>();
|
||||
ite:
|
||||
for (RequestHandler handler : requestHandlers) {
|
||||
if (handler instanceof WebMvcRequestHandler){
|
||||
WebMvcRequestHandler requestHandler = (WebMvcRequestHandler) handler;
|
||||
HandlerMethod handlerMethod = requestHandler.getHandlerMethod();
|
||||
Method method = handlerMethod.getMethod();
|
||||
Annotation[] annotations = method.getDeclaredAnnotations();
|
||||
Map map = null;
|
||||
for (Annotation annotation : annotations) {
|
||||
if (annotation instanceof Log || annotation instanceof io.swagger.annotations.ApiOperation){
|
||||
map = new HashMap();
|
||||
Class<? extends Annotation> aClass = annotation.getClass();
|
||||
try {
|
||||
Method value1 = ((Class) aClass.getGenericInterfaces()[0]).getDeclaredMethod("value");
|
||||
value1.setAccessible(true);
|
||||
Object invoke = value1.invoke(annotation);
|
||||
map.put("接口名称",invoke);
|
||||
break ;
|
||||
}catch (Exception ex){
|
||||
System.out.println(ex.getMessage());
|
||||
continue ite;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (map==null){
|
||||
continue ;
|
||||
}
|
||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
String[] params = new String[parameterTypes.length];
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
params[i] = parameterTypes[i].getName().substring(parameterTypes[i].getName().lastIndexOf('.')+1);;
|
||||
}
|
||||
map.put("请求参数",Arrays.toString(params));
|
||||
Set<String> patterns = requestHandler.getRequestMapping().getPatternsCondition().getPatterns();
|
||||
Set<RequestMethod> methods = requestHandler.getRequestMapping().getMethodsCondition().getMethods();
|
||||
map.put("请求接口",patterns.toString());
|
||||
map.put("请求类型",methods.toString());
|
||||
map.put("后端代码入口",handlerMethod.getBean());
|
||||
Api api = handlerMethod.getBeanType().getDeclaredAnnotation(Api.class);
|
||||
String interfaceType = "";
|
||||
if (api!=null){
|
||||
interfaceType = api.tags()[0];
|
||||
}
|
||||
map.put("接口类型",interfaceType );
|
||||
urlMapping.add(map);
|
||||
}
|
||||
}
|
||||
ApiDocScan.writeInterfaceFile(urlMapping);
|
||||
}
|
||||
|
||||
public static void writeInterfaceFile(List<Map> list ) {
|
||||
try {
|
||||
String tempPath = System.getProperty("user.dir") + File.separator + IdUtil.fastSimpleUUID() + ".xlsx";
|
||||
File file = new File(tempPath);
|
||||
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
|
||||
writer.write(list, true);
|
||||
SXSSFSheet sheet = (SXSSFSheet)writer.getSheet();
|
||||
sheet.trackAllColumnsForAutoSizing();
|
||||
//列宽自适应
|
||||
writer.autoSizeColumnAll();
|
||||
writer.close();
|
||||
System.out.println("输出完成,文件地址:"+tempPath);
|
||||
}catch (Exception ex){
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void stop() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user