Spring serving static content while having wildcard controller route

后端 未结 1 1039
悲哀的现实
悲哀的现实 2021-02-08 07:08

My application is build using backbone on frontend and spring framework on backend. It is a single html application. Routes are handled by backbone, so I have a backend route wi

1条回答
  •  借酒劲吻你
    2021-02-08 07:54

    The first thing is that your controller method that's mapped to /** will be taking priority over any resource requests. You can address this by increasing the precedence of ResourceHandlerRegistry. Add a call to registry.setOrder(Ordered.HIGHEST_PRECEDENCE) in the addResourceHandlers method of StaticResourceConfiguration:

    @Configuration
    public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
            registry.addResourceHandler("/js/**").addResourceLocations("/resources/js");
        }
    }
    

    The second thing is that, by default, Spring Boot configures two resource handlers for you by default, one mapped to /** and one mapped to /webjars/**. Due to the change described above, this will now take priority over the method in your controller that's also mapped to /**. To overcome this, you should turn off default resource handling via a setting in application.properties:

    spring.resources.addMappings=false
    

    0 讨论(0)
提交回复
热议问题