Reputation: 1291
Started programming in SpringBoot coming from Rails and can't fine the equivalent to before_filter. I just want to add simple functions that happen before my controllers get called
Upvotes: 1
Views: 252
Reputation: 90507
What you are looking for is HandlerInterceptor
which have the following methods allow you to execute some codes when some events happens:
preHandle(..)
: Before the actual controller method is executedpostHandle(..)
: After the controller method is executedafterCompletion(..)
: After the complete request has finishedOnce you create a HandlerInterceptor
, you can register by adding it to the InterceptorRegistry
:
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyHandlerInterceptor());
}
}
Upvotes: 3