Totic
Totic

Reputation: 1291

Is there an Equivalent to before_filter from rails in SpringBoot?

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

Answers (1)

Ken Chan
Ken Chan

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 executed
  • postHandle(..): After the controller method is executed
  • afterCompletion(..): After the complete request has finished

Once 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

Related Questions