With the idea of action, the first step must be to see if someone else has solved the problem. Looked for a half-day data, but found that there are several similar problems, but there is no satisfactory answer, had to debug spring itself, tracking its processing steps, finally let me find a feasible way, not to say, direct code:
1, first set up a class to save the things I Want
private 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, then is to collect information to create objects
@RequestMapping (value = "/index", method =requestmethod.get) PublicModelandview Index (httpservletrequest request) {ServletContext ServletContext=request.getsession (). Getservletcontext (); if(ServletContext = =NULL) { return NULL; } webapplicationcontext AppContext=Webapplicationcontextutils.getwebapplicationcontext (ServletContext); //mapping of request URLs and processing methodsList<requesttomethoditem> requesttomethoditemlist =NewArraylist<requesttomethoditem>(); //get all the requestmappingmap<string, handlermapping> allrequestmappings = Beanfactoryutils.beansoftypeincludingancestors (AppContext,
Handlermapping.class,true,false); for(handlermapping handlerMapping:allRequestMappings.values ()) {//only URL mappings in requestmappinghandlermapping are required for this project if(handlermappinginstanceofrequestmappinghandlermapping) {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=Setutils.first (Methodcondition.getmethods ()). Name (); Patternsrequestcondition patternscondition=requestmappinginfo.getpatternscondition (); String Requesturl=Setutils.first (Patternscondition.getpatterns ()); String controllername=Mappinginfovalue.getbeantype (). toString (); String Requestmethodname=Mappinginfovalue.getmethod (). GetName (); Class<?>[] Methodparamtypes =Mappinginfovalue.getmethod (). Getparametertypes (); Requesttomethoditem Item=NewRequesttomethoditem (Requesturl, RequestType, Controllername, Requestmethodname,
methodparamtypes); Requesttomethoditemlist.add (item); } Break ; } } return New Modelandview ("index"). AddObject ("MethodList", requesttomethoditemlist); }
This step I need to explain, I only use the "requestmappinghandlermapping" in the mapping information, in fact, there are three other handlermapping, if you want to get all the mapping information in the project, it must be one can not let go. The debugging information is as follows:
3, after taking the data to display Bai, with the help of Thymeleaf, it is easy to achieve the display, the effect is the first picture.
<!DOCTYPE HTML><HTMLxmlns:th= "http://www.thymeleaf.org"><Head> <title>Spring Thyme Seed Starter Manager</title> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/> <Linkrel= "stylesheet"type= "Text/css"Media= "All"href=".. /.. /css/stsm.css "Th:href= "@{/css/stsm.css}"/> <title>Request Method List</title></Head><Body><Divstyle= "Margin:0;padding:0;text-align:center"><H1>Request Method List</H1></Div><Div> <ul> <LiTh:each= "Method:${methodlist}"> <H3Th:text= "${method.methodname}"></H3> <PTh:text= "' Owning controller: ' +${method.controllername} '></P> <PTh:text= "' Request URL: ' +${method.requesturl} '></P> <PTh:text= "' request method: ' +${method.requesttype} '></P> <Div> <P>Method parameter list:</P> <ul> <LiTh:each= "Param:${method.methodparmatypes}"> <PTh:text= "${param}"></P> </Li> </ul> </Div> </Li> </ul></Div></Body></HTML>