整理试用了网上推荐的两种获取springboot框架下所有URL的方法,并稍作调整,直接上代码:
方法1:
package com.xxx.util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class UrlCollector {
@Data
@AllArgsConstructor
@NoArgsConstructor
private class UrlInfo {
private String url;
private String method;
private String desc;
}
@Autowired
WebApplicationContext applicationContext;
@RequestMapping(value = "v1/getAllUrl", method = RequestMethod.GET)
public Object getAllUrl() {
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
// 获取url与类和方法的对应信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
List<UrlInfo> list = new ArrayList<UrlInfo>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
UrlInfo urlInfo = new UrlInfo();
RequestMappingInfo info = m.getKey();
HandlerMethod method = m.getValue();
PatternsRequestCondition p = info.getPatternsCondition();
for (String url : p.getPatterns()) {
urlInfo.setUrl(url);
}
// map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
// urlInfo.setMethod(method.getMethod().getName()); // 方法名
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
urlInfo.setMethod(requestMethod.toString());
}
list.add(urlInfo);
}
return list;
}
}
方法二:
package com.xxx.util;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
public class UrlCollector2 {
// @Value("${server.servlet.context-path}") //如有
// public String system_code;
@Data
@AllArgsConstructor
@NoArgsConstructor
private class UrlInfo {
private String url;
private String method;
private String desc;
}
@Autowired
WebApplicationContext applicationContext;
@RequestMapping(value = "v2/getAllUrl", method = RequestMethod.GET)
public Object getAllUrl() {
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
List<UrlInfo> list = new ArrayList<UrlInfo>();
for (String beanName : beans.keySet()) {
Object value = applicationContext.getBean(beanName);
if (value == null) {
continue;
}
Method[] functions = value.getClass().getMethods();
for (Method func : functions) {
UrlInfo urlInfo = new UrlInfo();
//每个方法必定含有下面的注解中的其中一个
// ApiOperation apiOperation = AnnotationUtils.findAnnotation(func, ApiOperation.class); //测试时这里我会报错,原因未知
// if (apiOperation != null) {
// urlInfo.setDesc(apiOperation.value());
// }
String url = "";
String reqMethod = "";
RequestMapping reqMapping = AnnotationUtils.findAnnotation(func, RequestMapping.class);
PostMapping postMapping = AnnotationUtils.findAnnotation(func, PostMapping.class);
GetMapping getMapping = AnnotationUtils.findAnnotation(func, GetMapping.class);
PutMapping putMapping = AnnotationUtils.findAnnotation(func, PutMapping.class);
DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(func, DeleteMapping.class);
if (postMapping != null) {
url = postMapping.value()[0];
reqMethod = "POST";
} else if (getMapping != null) {
url = getMapping.value()[0];
reqMethod = "GET";
} else if (putMapping != null) {
url = putMapping.value()[0];
reqMethod = "PUT";
} else if (deleteMapping != null) {
url = deleteMapping.value()[0];
reqMethod = "DELETE";
} else if (reqMapping != null) { //mapping 顺序一定要在后面
url = reqMapping.value()[0];
RequestMethod[] reqMethods = reqMapping.method();
for (RequestMethod req : reqMethods) {
if(reqMethod.equals("")){
reqMethod = req.toString();
}
else{
reqMethod = reqMethod +";"+ req.toString();
}
}
} else {
continue;
}
urlInfo.setUrl(url);
urlInfo.setMethod(reqMethod);
//url信息入库
list.add(urlInfo);
}
}
return list;
}
}