Specify invocation order of filters in Spring Boot
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component @Order(0) public class FirstFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; System.out.println("Request URI is: " + req.getRequestURI()); chain.doFilter(request, response); System.out.println("Response Status Code is: " + res.getStatus()); } } |
SecondFilter.java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component @Order(1) public class SecondFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; System.out.println("Request Query String is: " + req.getQueryString()); chain.doFilter(request, response); System.out.println("Response Content Type is: " + res.getContentType()); } } |
Controller.java
|
1 2 3 4 5 6 7 8 9 10 |
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller { @RequestMapping("/execute") String execute() { return "SUCCESS"; } } |
Now when any HTTP request is made, the filters are invoked in the desired order, as shown below:
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)