Core interface HandlerInteceptorHandlerInteceptor main method 1. boolean preHandle(): Preprocessing is performed before the Controller method is executed. If true is returned, the method processing will continue, otherwise the method processing will be terminated; postHandle and

2024/05/1914:05:33 technology 1878

core interface

HandlerInteceptor

HandlerInteceptor main method

1, boolean preHandle(): Controller method is preprocessed before execution. If true is returned, the method processing will continue, otherwise the method processing will be terminated; postHandle and afterCompletion will not be entered.

2, void postHandle(): Returns true after the preHandle call of all interceptors is completed, and is not called until the controller method is executed but before the view is rendered. The order is opposite to that specified by order.

3, void afterCompletion(): After all interceptor preHandle calls are completed and true is returned, all postHandle execution is completed, and

is executed after the view rendering is completed. Using

1, first write the interceptor logic implementation class

package com.falcon.design.interceptor;import lombok.extern.slf4j.Slf4j;import org.springframework.util.StopWatch;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Slf4jpublic class DesignIntercept implements HandlerInterceptor {private ThreadLocalStopWatch stopWatchThreadLocal = new ThreadLocal();@Overridepublic boolean preHandle(HttpServletRequest request, HttpServlet Response response, Object handler) throws Exception {StopWatch stopWatch = new StopWatch();stopWatchThreadLocal.set(stopWatch);stopWatch.start();return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {stopWatchThreadLocal.get( ).stop();stopWatchThreadLocal.get().start();}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {StopWatch sw = stopWatchThreadLocal.get();String method = handler .getClass().getSimpleName();if(handler instanceof HandlerMethod){String beanType = ((HandlerMethod) handler).getBeanType().getName();String methodName = ((HandlerMethod) handler).getMethod().getName( );method = beanType + "." + methodName;}log.info("{}, {}, {}, {}; {}ms, {}ms, {}ms",request.getRequestURI(),method ,response.getStatus(), ex==null ? "-" : ex.getClass().getSimpleName(),sw.getTotalTimeMillis(), sw.getTotalTimeMillis()-sw.getLastTaskTimeMillis(), sw.getLastTaskTimeMillis()) ;stopWatchThreadLocal.remove();}}

2. Add to the interception container

package com.falcon.design.config;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import com.falcon in the private webMvcConfiguer through the addInterceptor method. design.interceptor.DesignIntercept;import org.springframework.boot.autoconfigure.http.HttpMessageConverters;import org.springframework.context.annotation.Configuration;import org.springframework.http.MediaType;import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.ArrayList;import java.util.List;@Configurationpublic class WebMvcConfig implements WebMvcConfigurer {public HttpMessageConverters FastJsonHttpMessageConverters(){return new HttpMessageConverters( stringHttpMessageConverter(), new FastJsonHttpMessageConvertExtension());} public class FastJsonHttpMessageConvertExtension extends FastJsonHttpMessageConverter{FastJsonHttpMessageConvertExtension(){List MediaType mediaTypes = new ArrayList();mediaTypes.add(MediaType.APPLICATION_JSON);setSupportedMediaTypes (mediaTypes);}}public StringHttpMessageConverter stringHttpMessageConverter(){return new StringHttpMessageConverter();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new DesignIntercept()).addPathPatterns("/design/**");} }

3, test

Core interface HandlerInteceptorHandlerInteceptor main method 1. boolean preHandle(): Preprocessing is performed before the Controller method is executed. If true is returned, the method processing will continue, otherwise the method processing will be terminated; postHandle and  - DayDayNews

If this article solves your doubts, please don’t mess with me in vain, try a one-click three-way

technology Category Latest News