Java拦截器未被调用
我有一个Spring启动应用程序,我正在实现和侦听器,以便记录一些数据。 问题是没有人打电话,我试过:Java拦截器未被调用,java,spring,spring-boot,interceptor,Java,Spring,Spring Boot,Interceptor,我有一个Spring启动应用程序,我正在实现和侦听器,以便记录一些数据。 问题是没有人打电话,我试过: @Interceptor public class LoggerInterceptor{ @AroundInvoke public Object collectBasicLoggingInformation(InvocationContext context) throws Exception { Logger logger = LoggerFactory.g
@Interceptor
public class LoggerInterceptor{
@AroundInvoke
public Object collectBasicLoggingInformation(InvocationContext context) throws Exception {
Logger logger = LoggerFactory.getLogger(context.getClass());
logger.info("Method Called: " + context.getMethod()
.getName());
logger.info("Parameters: " + Arrays.toString(context.getParameters()));
return context.proceed();
}
}
然后我应用于方法或类,但在这两种方法或类中都不起作用:
@GetMapping
@Interceptors(LoggerInterceptor.class)
public List getAllFilingNumber(){
logger.info("This is a test");
return filingNumberService.findAll();
}
或
有人知道我做错了什么吗
谢谢如果您拥有springboot应用程序为了拦截对控制器的请求,您必须采取不同的方法 拦截器与Java EE托管类一起使用来 允许开发人员在相关目标上调用拦截器方法 类,与方法调用或生命周期事件结合使用。 拦截器的常见用途是日志记录、审核和分析。 参考文献 您正在尝试将Java EE注释与spring一起使用,但这不起作用。在spring引导中,您必须注册拦截器,如:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LocaleChangeInterceptor());
registry.addInterceptor(new ThemeChangeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
}
}
拦截器本身必须是一个类,它扩展了HandlerInterceptorAdapter
,并实现如下方法。
从春天开始:
所有HandlerMapping实现都支持
当您要将特定功能应用于某些特定的
请求 — 例如,检查主体。拦截器必须
从org.springframework.web.servlet实现HandlerInterceptor
包含三种方法的包,应提供足够的灵活性
进行各种前处理和后处理:
preHandle(..): Before the actual handler is executed
postHandle(..): After the handler is executed
afterCompletion(..): After the complete request has finished
preHandle(..): Before the actual handler is executed
postHandle(..): After the handler is executed
afterCompletion(..): After the complete request has finished
@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object object) throws Exception {
System.out.println("we are Intercepting the Request");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object object, ModelAndView model)
throws Exception {
System.out.println("request processing "
+ "completed by @RestController");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object object, Exception arg3)
throws Exception {
System.out.println("afterCompletion Request Completed");
}
}