Spring - 设置HandlerMapping优先级

时间:2013-06-28 22:24:52

标签: web-applications spring-mvc

如何在spring中设置处理程序映射的优先级以允许资源处理程序在控制器请求映射之前进行映射?

例如此配置:

@Configuration
@EnableWebMvc
@ComponentScan("org.commons.sandbox")
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

使用以下控制器:

@Controller
public class HomeController {

    @RequestMapping("/**")
    public String home(){
        return "home";
    }
}

捕获对“/ resources”的请求。因此,链接到css文件是jsp返回“home”视图而不是“resources”目录中的实际css文件。我知道这是由于映射“/ **”,但我认为有一种方法可以配置映射顺序......是吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

这是我的解决方案......现在。

我必须直接从WebMvcConfigurationSupport延伸,而不是使用@EnableWebMvc

这是我更新的配置

@Configuration
@ComponentScan("org.commons.sandbox")
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
        //just to be clear that this has a priority of 0, this is default anyway
        registry.setOrder(0);
    }

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setViewClass(JstlView.class);
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handler = super.requestMappingHandlerMapping();
            //now i have a handle on the handler i can lower it's priority
            //in the super class implementation this is set to 0 
        handler.setOrder(1);
        return handler;
    }
}

我现在可以将RequestMappingHandlerMapping的优先级设置为低于资源处理程序的SimpleUrlHandlerMapping,以便在控制器类获得机会之前解析静态资源请求

答案 1 :(得分:0)

就我而言,我需要在SimpleUrlHandlerMapping之前ResourceHandler。 由setOrder()解决。 Spring Boot代码:

@EnableAutoConfiguration
@Configuration
@Slf4j
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Bean(name = "urlViewController")
    public UrlFilenameViewController getUrlViewController() {
        return new UrlFilenameViewController();
    }

    @Bean
    public SimpleUrlHandlerMapping getUrlHandlerMapping() {
        SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
        handlerMapping.setMappings(new Properties() {
            {
                put("/view/*", "urlViewController");
            }
        });
        handlerMapping.setOrder(Integer.MAX_VALUE - 5);
        return handlerMapping;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("file:./resources/static/");
        ....
        registry.setOrder(Integer.MAX_VALUE);
    }
}