【问题标题】:Spring Boot url mappings order for controllers and static pages控制器和静态页面的 Spring Boot url 映射顺序
【发布时间】:2015-09-18 09:58:18
【问题描述】:

我有一个 Spring Boot Web 应用程序,该应用程序旨在为静态和基于控制器的 (ModelAndView) 页面提供服务。问题是控制器可以提供类似/{string} 的服务,而静态页面必须使用/test 提供服务。

问题是控制器映射优先,我需要避免这种情况。如果用户点击/test,他必须被转发到test.html静态页面。

我尝试用这种方式使用ViewControllerRegistry的order属性,没有成功:

@Configuration
public class MyWebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/test").setViewName("forward:/test.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE); // but I tried with 0 and -1 as well: annotated controllers should have order equals to 0
    }

}

这是我的 SpringBootApplication 类:

@SpringBootApplication
public class VipApplication {

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

}

这是控制器代码:

@Controller
public class VipController {

    @RequestMapping(value = "/{string}")
    public ModelAndView vip(@PathVariable("string") String string) {
        ModelAndView mv = new ModelAndView("mypage");
        return mv;
    }

}

如何重新排序映射以确保在带注释的控制器之前考虑静态页面?

【问题讨论】:

    标签: spring spring-mvc spring-boot


    【解决方案1】:

    (我不确定,但是)我建议重写WebMvcConfigurerAdapter.addResourceHandlers() 方法并通过调用ResourceHandlerRegistry.setOrder() 来配置资源处理程序的顺序

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2013-11-26
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多