How to specify invocation order of filters in Spring Boot

In this post we will see how to specify invocation order of filters in a Spring Boot application.


 

In previous post, we have discussed how to create a filter in Spring Boot by implementing the Filter interface. In this post, we will see how to specify invocation order of filters in a Spring Boot.

Let’s consider two filter class – FirstFilter and SecondFilter. Both classes implements the Filter interface and defined as a bean with the @Component annotation. In order to specify the invocation order of filters, we need to use the @Order annotation. This is demonstrated below:

 

FirstFilter.java

 

SecondFilter.java

 

Controller.java

 
Now when any HTTP request is made, the filters are invoked in the desired order as shown below:

Request URI is: /execute
Request Query String is: null
Response Content Type is: text/html;charset=UTF-8
Response Status Code is: 200

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Leave a Reply

avatar
  Subscribe  
Notify of