This post will discuss how to specify the invocation order of filters in a Spring Boot application.

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

Let’s consider two filter class – FirstFilter and SecondFilter. Both classes implement the Filter interface and are defined as a bean with the @Component annotation. 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

That’s all about specifying the invocation order of filters in Spring Boot.