1.sprongboot获取所有的url和对应类和方法以及请求类型 代码如下:
package com
.es
.config
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.stereotype
.Controller
;
import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
import org
.springframework
.web
.bind
.annotation
.ResponseBody
;
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
.*
;
@Controller
public class getAllUrlController {
@Autowired
WebApplicationContext applicationContext
;
@ResponseBody
@RequestMapping("/getAllUrl")
public List
<Map
<String, String>> getAllUrl() {
RequestMappingHandlerMapping mapping
= applicationContext
.getBean(RequestMappingHandlerMapping
.class);
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> mapone
= new HashMap<String, String>();
RequestMappingInfo info
= m
.getKey();
HandlerMethod method
= m
.getValue();
PatternsRequestCondition p
= info
.getPatternsCondition();
for (String url
: p
.getPatterns()) {
mapone
.put("url", url
);
}
mapone
.put("className", method
.getMethod().getDeclaringClass().getName());
mapone
.put("method", method
.getMethod().getName());
RequestMethodsRequestCondition methodsRequestCondition
= info
.getMethodsCondition();
for (RequestMethod requestMethod
: methodsRequestCondition
.getMethods()) {
mapone
.put("type", requestMethod
.toString());
}
list
.add(mapone
);
}
return list
;
}
}
可用于权限控制url的获取和权限标识(权限标识一般根据url生成)
参考 :添加链接描述