Get all URLs to controller method mappings in the SPRINGMVC project

Source: Internet
Author: User

Spring is a great and powerful open source framework that is like a container that provides us with a variety of bean components and services. For the MVC part, it implements the logic processing from the URL request Map Controller method, in our usual development work does not need too much to ignore this URL is how to map with the method in the controller, all by the spring MVC to help us fix.

But when I was doing the SDK project today, I suddenly had an idea: can I print out all the URLs and method mappings in my project? So I can see at a glance that I have completed those API interface development, what parameters these methods require. Just like the following:

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>

Get all URLs to controller method mappings in the SPRINGMVC project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.