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:

3_spring-boot-filter.zip

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.

Explain:

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:

package com.surasint.example.web.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
public class ExampleFilter implements Filter{
    private static final Logger logger = LoggerFactory.getLogger(ExampleFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("filter:"+ ((HttpServletRequest)servletRequest).getRequestURL());
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

The keys here are :

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

Try:

run this in console:

mvn clean package

Then run this to start:

mvn clean spring-boot:run

You should see something like this:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

.....
.....
2017-12-12 22:03:04.243  INFO 260 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.Annotation
ConfigEmbeddedWebApplicationContext@7595f8d7: startup date [Tue Dec 12 22:03:01 CET 2017]; root of context hierarchy

Open a browswer and try to go to this url:

http://localhost:8080/api/count

In the console, you should see this:

2017-12-16 11:10:16.355  INFO 9876 --- [nio-8080-exec-1] c.s.example.web.filter.ExampleFilter     : filter:http://localhost:8080/api/count

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