GithubHelp home page GithubHelp logo

Comments (2)

snicoll avatar snicoll commented on April 28, 2024

Thanks for the report but that's the wrong issue tracker. Moving to Spring Boot.

From a quick debugging session, those filters are indeed taken into account by SpringBootMockMvcBuilderCustomizer but their urlPatterns collection is empty.

from spring-boot.

wilkinsona avatar wilkinsona commented on April 28, 2024

Mixing Spring's component model and the Servlet spec's component model isn't supported. This means that your sample also doesn't work as you would like when running its main method as the url patterns are ignored there too.

If you want to use @WebFilter you should not use @Component. Instead, you should enable scanning for servlet components using @ServletComponentScan:

@ServletComponentScan
@SpringBootApplication
public class WebfilterBugApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebfilterBugApplication.class, args);
    }

    @WebFilter("/not/simple")
    public static class SimpleFilterOne extends OncePerRequestFilter {

        public static boolean CALLED = false;
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        	System.out.println("One");
            CALLED = true;
            filterChain.doFilter(request, response);
        }
    }

    @WebFilter
    public static class SimpleFilterTwo extends OncePerRequestFilter {

        public static boolean CALLED = false;
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        	System.out.println("Two");
            CALLED = true;
            filterChain.doFilter(request, response);
        }
    }

    @WebFilter(urlPatterns = "/not/simple")
    public static class SimpleFilterThree extends OncePerRequestFilter {

        public static boolean CALLED = false;
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        	System.out.println("Three");        	
            CALLED = true;
            filterChain.doFilter(request, response);
        }
    }

    @RestController
    @RequestMapping("/rest/simple")
    public static class SimpleController {


        @GetMapping
        public String getString() {
            return "Hello World";
        }
    }
}

This will result in the @WebFilters being found and their attributes being honored when it's started using its main method. Unfortunately, it doesn't fix the tests as I have just discovered that the registration of servlet components doesn't work in a mock web environment. It works with @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) but not with the default mock environment. We can use this issue to fix that.

from spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.