The Java Web Project uses the structure of the spring+spring mvc+hibernate, and the methods in the controller are used to process the front-end access information. The controller returns a Modelandview object to the front end or only JSON-formatted data by invoking the service for business processing. It is convenient for development and debugging if you can get information about HTTP requests that are processed in the backend program. Interceptor, which uses spring MVC in the project, intercepts all HTTP requests and their responses to obtain information about the access interface and the length of the program's processing, specifically documenting how it is implemented.
1 PackageCom.api.web.interceptor;2 3 Importjava.util.Arrays;4 Importjava.util.Date;5 ImportJava.util.Map;6 ImportJava.util.Map.Entry;7 8 Importjavax.servlet.http.HttpServletRequest;9 ImportJavax.servlet.http.HttpServletResponse;Ten One ImportOrg.apache.log4j.Logger; A ImportOrg.springframework.web.method.HandlerMethod; - ImportOrg.springframework.web.servlet.HandlerInterceptor; - ImportOrg.springframework.web.servlet.ModelAndView; the - ImportCom.yijia.api.util.SimpleDateFormatCache; - - /** + * Record information:</br> access time </br>controller path </br> corresponding method name </br> request parameter information </br> request relative path </br> Request Processing Duration - * + * @authorAdministrator A * at */ - Public classTimecostinterceptorImplementsHandlerinterceptor { - - //before the actual handler 'll be executed - Public BooleanPrehandle (httpservletrequest request, -HttpServletResponse response, Object handler)throwsException { in LongStartTime =System.currenttimemillis (); -Request.setattribute ("StartTime", startTime); to if(HandlerinstanceofHandlermethod) { +StringBuilder SB =NewStringBuilder (1000); - theSb.append ("-----------------------"). Append (Simpledateformatcache.getymdhms (). Format (NewDate ())) *. Append ("-------------------------------------\ n"); $Handlermethod h =(Handlermethod) handler;Panax NotoginsengSb.append ("Controller:"). Append (H.getbean (). GetClass (). GetName ()). Append ("\ n"); -Sb.append ("Method:"). Append (H.getmethod (). GetName ()). Append ("\ n"); theSb.append ("Params:"). Append (Getparamstring (Request.getparametermap ())). Append ("\ n"); +Sb.append ("URI:"). Append (Request.getrequesturi ()). Append ("\ n"); A System.out.println (sb.tostring ()); the } + return true; - } $ $ //After the handler is executed - Public voidPosthandle (httpservletrequest request, - httpservletresponse Response, Object handler, theModelandview Modelandview)throwsException { - LongStartTime = (Long) request.getattribute ("StartTime");Wuyi LongEndTime =System.currenttimemillis (); the LongExecutetime = EndTime-StartTime; - if(HandlerinstanceofHandlermethod) { WuStringBuilder SB =NewStringBuilder (1000); -Sb.append ("Costtime:"). Append (Executetime). Append ("MS"). Append ("\ n"); AboutSb.append ("-------------------------------------------------------------------------------"); $ System.out.println (sb.tostring ()); - } - } - A PrivateString getparamstring (map<string, string[]>map) { +StringBuilder SB =NewStringBuilder (); the for(entry<string,string[]>E:map.entryset ()) { -Sb.append (E.getkey ()). Append ("="); $String[] Value =E.getvalue (); the if(Value! =NULL&& Value.length = = 1){ theSb.append (Value[0]). Append ("\ t"); the}Else{ theSb.append (arrays.tostring (value)). Append ("\ t"); - } in } the returnsb.tostring (); the } About the Public voidaftercompletion (HttpServletRequest arg0, the httpservletresponse arg1, Object arg2, Exception arg3) the throwsException { + - } the}
Since interceptor is a functional component of spring MVC, this interceptor needs to be configured in the SPRINGMVC XML configuration file (or using other configuration methods such as Javaconfig):
<mvc:interceptors> <!--using beans to define a interceptor, directly defined under the Mvc:interceptors root interceptor will intercept all requests-- <mvc:interceptor> <mvc:mapping path= "/**"/> <!--need to exclude the blocked address-- <MVC: Exclude-mapping path= "/"/> <mvc:exclude-mapping path= "/test"/> <!--defined in MVC: Interceptor the following representation is blocked for a specific request-- class= "Com.api.web.interceptor.TimeCostInterceptor"/> </mvc:interceptor> </mvc:interceptors>
After a good interceptor is defined and configured, the relevant access information is output to the console when the interface in the program controller is accessed, and logs are used to log access information to the log.
The recording effect is as follows:
Summarize:
Interceptors intercept user requests by preprocessing the user's request (HttpServletRequest), or by accessing the response (HttpServletResponse) returned to the user, and then processing the requested exception information after the request is completed. The problems that can be manipulated in the request object and the response object can be handled uniformly by the interceptor, including user Login control, permission check, cross-domain request access control and so on.
Logging of controller Interface access information in spring MVC via interceptor Interceptor