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:

JavaScript

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

JavaScript

Or

JavaScript

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 :

JavaScript

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:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement