@RequestMapping(value = "/index", method = RequestMethod.GET)
public List<RequestToMethodItem> index(HttpServletRequest request)
{
ServletContext servletContext = request.getSession().getServletContext();
if (servletContext == null)
{
return null;
}
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//请求url和处理方法的映射
List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();
//获取所有的RequestMapping
Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext,
HandlerMapping.class, true, false);
for (HandlerMapping handlerMapping : allRequestMappings.values())
{
//本项目只需要RequestMappingHandlerMapping中的URL映射
if (handlerMapping instanceof RequestMappingHandlerMapping)
{
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())
{
RequestMappingInfo requestMappingInfo = requestMappingInfoHandlerMethodEntry.getKey();
HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();
RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition();
String requestType = methodCondition.getMethods().stream().map(vo -> String.valueOf(vo)).collect(Collectors.joining(","));
PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
// String requestUrl = SetUtils.first(patternsCondition.getPatterns());
String requestUrl = patternsCondition.getPatterns().stream().map(vo -> String.valueOf(vo)).collect(Collectors.joining(","));;
String controllerName = mappingInfoValue.getBeanType().toString();
String requestMethodName = mappingInfoValue.getMethod().getName();
Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes();
RequestToMethodItem item = new RequestToMethodItem(requestUrl, requestType, controllerName, requestMethodName,
methodParamTypes);
requestToMethodItemList.add(item);
}
}
}
return requestToMethodItemList;
}
springboot获取所有的请求列表
最新推荐文章于 2024-06-14 15:22:37 发布