A tool class that locates the spring MVC map code based on the URI

Source: Internet
Author: User

@Controller @requestmapping ("/admin/util") public class systemcontroller {     private static final logger log = loggerfactory.getlogger (SystemController  .class);         @RequestMapping (value =  "/ Findurimapmethod.do ")      @ResponseBody     public String  Findurimapmethod (Httpservletrequest request, httpservletresponse response)  {         final env env = envutils.getenv ();         final string uri = env.param ("uri",  Request.getrequesturi ());         return gethandler (request,  uri,  "GET");     }    private string gethandler ( Httpservletrequest request, final string uRi, string method)  {        final env env  = envutils.getenv ();         final string fmethod  = method;        String[] beanNames =  Env.getapplicationcontext (). Getbeannamesfortype (Requestmappinghandlermapping.class);         log.info ("requestmappinghandlermapping: {}",  arrays.tostring (BeanNames));         HttpServletRequestWrapper httpServletRequestWrapper  = new httpservletrequestwrapper (Request)  {              @Override              Public string getrequesturi ()  {                 /*stRing paramuri = super.getparameter ("uri");                 if (paramuri != null && ! "". Equals (Paramuri.trim ()))  {                     return paramUri;                 }*/                 return uri;             }                          @Override              public stringbuffer getrequesturl ()  {                  return new stringbuffer (Super.getrequesturl (). toString ()                  .replace ( Super.getrequesturi (),  uri));             }                           @Override              public string getservletpath ()  {                 return super.getservletpath (). Replace (Super.getRequestURI (),  uri);            }                           @Override &NBSP;&NBSP;&NBSp;         public string getmethod ()  {                 if (fMethod ==  null | |   ". Equals (Fmethod))  {                     return super.getmethod ();                 }                 return fMethod;             }        };         stringbuilder urimapmethod = new stringbuilder ();         urimapmethod.append (Httpservletrequestwrapper.getrequesturi ()). Append (":  ["); &nbsP;       if (beannames != null)  {             for (string beanname : beannames)  {                 log.info ("BeanName:  {}  ",  beanname);                 requestmappinghandlermapping mapping = env.getbean (beanName,                   Requestmappinghandlermapping.class);                 try {                     HandlerExecutionChain chain =  Mapping.gethandler (httpservletrequestwrapper);    &Nbsp;                if (Chain  != null)  {                         object handler = chain.gethandler () ;                   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (handler);                         if ( Handler instanceof handlermethod)  {                              handlermethod hm =  (Handlermethod) handler;             &nbsP;               log.info ("{}:{}",  hm.getbeantype (). GetName (), HM);                              Urimapmethod.append (HM);                         } else if (handler instanceof  Org.springframework.web.servlet.mvc.Controller)  {                              org.springframework.web.servlet.mvc.Controller hm =  ( Org.springframework.web.servlet.mvc.Controller) handler;                             class<? extends  Org.springframework.web.servlet.mvc.controller> hmclass = hm.getclass ();                              log.info ("{}:{}",  hmclass.getname (),  Hmclass.getdeclaredmethod ("HandleRequest",                               Httpservletrequest.class, httpservletresponse.class));                              urimapmethod.append (Hmclass.getdeclaredmethod ("HandleRequest",                              httpservletrequest.class,  Httpservletresponse.class));                         } else {                              urimapmethod.append (Handler.getclass (). GetName ());                          }                         break;                     }                 } catch  (httprequestmethodnotsupportedexception e)  {                      return  GetHandler (httpservletrequestwrapper, uri,  "POST");                 } catch  (exception e)  {                      Log.error ("Get uri mapping error.",  e);                  }                 /*map<requestmappinginfo, handlermethod> mapmethods  =  mapping.gethandlermethods ();                 if (mapmethods != null)  {                     Iterator<Entry<RequestMappingInfo,  Handlermethod>> iter = mapmethods.entryset (). Iterator ();                     while  ( Iter.hasnext ())  {                         entry<requestmappinginfo, handlermethod > entry = iter.next ();                         requestmappinginfo key  = entry.getkey ();                         handlermethod hm =  (Handlermethod) entry.getvalue ();                          method method = hm.getmethod ();                         log.info ("{}  : {}->{} ",  key.getpatternscondition (), &NBSP;KEY,&NBSP;HM);                     }                 }*/             }        }         return urimapmethod.append ("]"). toString ();     }}

It is convenient to locate the controller code for spring MVC based on the URI.

A tool class that locates the spring MVC map code based on the URI

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.