Add a Servlet Filter in a Spring Boot application
Asked Answered
M

2

25

I'd like to have ETag suport. For this purpose there is a ShallowEtagHeaderFilter which does all the work. How can I add it without declaring it in my web.xml (which actually does not exist, because I somehow got by without it so far)?

P.S. I use Spring Boot 1.1.4

P.P.S. Here's a full solution

package cuenation.api;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

import javax.servlet.DispatcherType;
import java.util.EnumSet;

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean shallowEtagHeaderFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new ShallowEtagHeaderFilter());
        registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
        registration.addUrlPatterns("/cue-categories");
        return registration;
    }

}
Mic answered 1/10, 2014 at 21:34 Comment(5)
Uhm, your question contains a link to the solution. Just scroll down a few lines.Susannesusceptibility
@zeroflagL Scroll down a few lines where: here or in the spring documentation? I'm not following you!Mic
@zeroflagL Oh, I see what you mean. But my problem is I don't have any xml configuration whatsoever. See my custom initializer. What am I doing wrong?Mic
duplicate question... #19826446Overwhelm
how to add ShallowEtagHeaderFilter to the project so that it can evaluate and send Etag to the browser. Do we need to override anything?Milly
O
37

When using Spring Boot

As mentioned in the reference documentation, the only step needed is to declare that filter as a Bean in a configuration class, that's it!

@Configuration
public class WebConfig {

  @Bean
  public Filter shallowEtagHeaderFilter() {
    return new ShallowEtagHeaderFilter();
  }
}

When using Spring MVC

You're probably already extending a WebApplicationInitializer. If not, then you should convert your webapp configuration from a web.xml file to a WebApplicationInitializer class.

If your context configuration lives in XML file(s), you can create a class that extends AbstractDispatcherServletInitializer - if using configuration classes, AbstractAnnotationConfigDispatcherServletInitializer is the proper choice.

In any case, you can then add Filter registration:

  @Override
  protected Filter[] getServletFilters() {
    return new Filter[] {
      new ShallowEtagHeaderFilter();
    };
  }

Full examples of code-based Servlet container initialization are available in the Spring reference documentation.

Overwhelm answered 2/10, 2014 at 9:6 Comment(12)
No I don't extend WebApplicationInitializer neither directly nor indirectly. Actually the only config I have is application.properties where I put my mongodb connection string. I tried to extend AbstractAnnotationConfigDispatcherServletInitializer but it requires to implement me a bunch of methods.Mic
Ok, I added my custom initializer. But looks like spring does not see it. What did I mess here?Mic
so you probably have a web.xml file somewhere... it's either web.xml (configuring your webapp with XML) or a WebApplicationInitializer. I'm adding a complete example in my answer.Overwhelm
No, as I said above I don't have any configuration files other than application.propertiesMic
Are you using Spring Boot then?Overwhelm
Yes, I use spring-boot-starter-web 1.1.4Mic
I've seen this 17.15 Code-based Servlet container initialization. My frustration comes because they use dispatcher-config.xml. I don't know what it is!Mic
updated my answer - you should have made clear from the start that you're using Spring Boot...Overwhelm
Hm, I added WebConfig class inside my Application but TomCat does not start saying Could not instantiate bean class [cuenation.api.Application$WebConfig$$EnhancerBySpringCGLIB$$6aed0802]: No default constructor found;Mic
Ok, I finally figured this outMic
Trying your approach spawned a new and different error, which warrants a new posting. Inserting your code is giving a No default constructor error. Are you willing to comment? Here is the link: #36534085Willdon
Is it same as if we register filters using configure(HttpSecurity http) by addFilterBefore and addFilterAfter methodsMartinsen
M
3

A bit late answer.

My solution was to create custom annotation:

import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;

// ...

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface Filter {

    @AliasFor(annotation = Component.class, attribute = "value")
    String value() default "";

}

And then simply apply it to the filter implementations:

@Filter
public class CustomFilter extends AbstractRequestLoggingFilter {

    @Override
    protected void beforeRequest(HttpServletRequest request, String message) {
        logger.debug("before req params:", request.getParameterMap());
    }

    @Override
    protected void afterRequest(HttpServletRequest request, String message) {
        logger.debug("after req params:", request.getParameterMap());
    }
}

See more: @AliasFor, Spring custom annotations question

Measureless answered 14/1, 2017 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.