1.建立一個實體類
public class RequestToMethodItem { public String controllerName; public String methodName; public String requestType; public String requestUrl; public Class<?>[] methodParmaTypes; public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName, Class<?>[] methodParmaTypes) { this.requestUrl = requestUrl; this.requestType = requestType; this.controllerName = controllerName; this.methodName = requestMethodName; this.methodParmaTypes = methodParmaTypes; } }
2.從springbean容器中獲取所有的實例bean封裝成list轉化成json輸出
@RequestMapping(value = "/xxx", method = RequestMethod.GET) public ModelAndView 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();
// 獲取請求方式GET/POST/PUT/DELETE(如果@RequestMapping中沒有寫具體的請求方式則獲取不到哦) RequestMethodsRequestCondition methodCondition = requestMappingInfo.getMethodsCondition(); Iterator<String> it = methodCondition.getMethods().iterator();
String requestType = it.hasNext()? it.next().name : "";
// 獲取請求的URL PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
Iterator<String> iterator = patternsCondition.getPatterns().iterator();
String requestUrl = iterator.hasNext()? iterator.next() : ""; //String requestUrl = SetUtils.first(patternsCondition.getPatterns());
//獲取接口所在的類名,方法名,參數名 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); } break; } }
//轉化成json打印輸出至控制台
String s=JSON.toJSONString(requestToMethodItemList);
System.out.println("List:"+s); return new ModelAndView("index").addObject("MethodList", requestToMethodItemList); }
3.結果
{ "controllerName": "class com.gy.abc.RPCService", "methodName": "readMessage", "methodParmaTypes": ["java.lang.Integer"], "requestType": "",//注意:如果注解中沒有寫明請求方式,則此處會為空 "requestUrl": "/xxx/xxx/message" }
備注:以上方法親測可用,不足之處還望指點!
以上內容參考:
https://www.cnblogs.com/yuananyun/archive/2014/08/25/3934371.html
https://www.cnblogs.com/zhang-cb/p/9890900.html
如需詳細了解RequestCondition請移步https://blog.csdn.net/andy_zhang2007/article/details/88913776