1 自定义拦截器
@Component
public class GlobalInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String path = request.getRequestURI();if (path.contains("/api/")) {path = path.replaceAll("/api/", "/");request.getRequestDispatcher(path).forward(request, response);}return true;}
}
2 注册拦截器
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {GlobalInterceptor globalInterceptor;@Autowiredpublic WebMvcConfig(GlobalInterceptor globalInterceptor) {this.globalInterceptor = globalInterceptor;}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(globalInterceptor).addPathPatterns("/api/**");}
}