Skip to content
Advertisement

Java interceptor not getting called

I have a Spring boot application and I am implementing and interceptor in order to log some data. The problem is that is not getting called, I have tried:

@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();
    }
}

And then I’ve applied to methods or classes and in both of them doesn’t work:

@GetMapping
@Interceptors(LoggerInterceptor.class)
public List getAllFilingNumber(){
    logger.info("This is a test");
    return filingNumberService.findAll();
}

Or

@RestController
@RequestMapping(FilingNumberController.BASE_URL)
@Interceptors(LoggerInterceptor.class)
public class FilingNumberController{

    @GetMapping
    public List getAllFilingNumber(){
        logger.info("This is a test");
        return filingNumberService.findAll();
    }
}

Does someone knows what I am doing wrong?

Thanks

Advertisement

Answer

If you are having a springboot application in order to intercept the request to a controller , you have to take a different approach altogethor.

Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling. Reference Doc

You are trying to use Java EE annotation with spring , which won’t work.In spring-boot you will have to register the interceptors like :

@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/*");
    }
}

The interceptor itself have to be a class which extends the HandlerInterceptorAdapter and implements the methods as follows. From Spring DOCS :

All HandlerMapping implementations support handler interceptors that are useful when you want to apply specific functionality to certain requests — for example, checking for a principal. Interceptors must implement HandlerInterceptor from the org.springframework.web.servlet package with three methods that should provide enough flexibility to do all kinds of pre-processing and post-processing:

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");
 }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement