Spring Boot with Filter – Example

You just want to see an minimal example of Spring boot wit Filter (or with Spring Boot Filter).

This page is for you.

NOTE: this is not maven filter, do not  be confused.

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 1.5.8.RELEASE:


What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Call an url  then a filter in the service will capture the request.


This is the file structure:

For pom.xml, Myapplicaton.java, TestRestController.java,  . They are the same as explain in here REST with Spring Boot , so I will not explain them.

In ExampleFilter.java, this filter will just print a message to console, the code looks like this:

The keys here are :

  • Line 7 : import javax.servlet.Filter
  • Line 17 :  implment Filter
  • Line 16 : use @Component to let Sping know it exists.


run this in console:

Then run this to start:

You should see something like this:

Open a browswer and try to go to this url:

In the console, you should see this:

That is what we want.

Do you notice something ? What about if I want to map with some pattern for example, only /api/* , then you need to look here Spring Boot with Filter with Url pattern

In this page, we are using Spring Boot Filter, which in another page Spring Boot with Filter with Url pattern , it is Servlet Filter. I would suggest you to use the Servlet Filter, not Spring Boot Filter.  I have explained it here Spring Boot : Difference between @WebFilter vs @Component