我有一个Spring Boot(v1.3.6)单页面应用程序(angular2),我想将所有请求转发给index.html
。
http://localhost:8080/index.html的请求正在运行(200,我得到index.html),但http://localhost:8080/home不是(404)。
Runner.class
@SpringBootApplication
@ComponentScan({"packagea.packageb"})
@EnableAutoConfiguration
public class Runner {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext run = SpringApplication.run(Runner.class, args);
}
}
WebAppConfig.class
@Configuration
@EnableScheduling
@EnableAsync
public class WebAppConfig extends WebMvcConfigurationSupport {
private static final int CACHE_PERIOD_ONE_YEAR = 31536000;
private static final int CACHE_PERIOD_NO_CACHE = 0;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(-1);
registry.addResourceHandler("/styles.css").addResourceLocations("/styles.css").setCachePeriod(CACHE_PERIOD_ONE_YEAR);
registry.addResourceHandler("/app/third-party/**").addResourceLocations("/node_modules/").setCachePeriod(CACHE_PERIOD_ONE_YEAR);
registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(CACHE_PERIOD_NO_CACHE);
registry.addResourceHandler("/systemjs.config.js").addResourceLocations("/systemjs.config.js").setCachePeriod(CACHE_PERIOD_NO_CACHE);
registry.addResourceHandler("/**").addResourceLocations("/index.html").setCachePeriod(CACHE_PERIOD_NO_CACHE);
}
}
styles.css
,/app/third-party/xyz/xyz.js
,..正在运行(200,我得到了正确的文件)。只有/**
到index.html
无效。
答案 0 :(得分:32)
您还可以添加转发控制器,如:
@Controller
public class ForwardingController {
@RequestMapping("/{path:[^\\.]+}/**")
public String forward() {
return "forward:/";
}
}
第一部分{path:[^\\.]+}
匹配.
以外的任何字符中的一个或多个。这样可以确保请求file.ext
的请求不会被此RequestMapping处理。如果您需要支持也可以转发的子路径,请将/**
放在{...}
之外。
答案 1 :(得分:2)
如果不查看日志,我并不完全确定为什么没有正确映射,但是如果要将URL映射到视图(HTML),那么最好使用viewController
机制spring提供http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-view-controller。 e.g。
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
(取自上面链接的spring文档 - 这是你应该如何将url映射到视图而不是重新设置静态资源的映射。)
我不确定资源映射是否有任何类型的后缀过滤 - 例如我不知道春天如何决定将请求映射到ResourceHttpRequestHandler
- 您是否尝试过(只是为了确认或否认)http://localhost:8080/home.html之类的内容是否适用于任何内容?
上面定义的html映射也可能被忽略,而index.html只是因为Spring-Boot的默认主页行为而工作:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java#L108 < / p>
答案 2 :(得分:0)
I had the same problem and the following worked for me. My html files are inside src/main/resources/static/app
The key was to remove @EnableWebMvc and add "classpath:/static/app/" to addResourceLocations! Hope this helps.
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/","classpath:/static/app/", "classpath:/public/" };
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations(
CLASSPATH_RESOURCE_LOCATIONS);
}
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// forward requests to /admin and /user to their index.html
registry.addViewController("/portal").setViewName(
"forward:/app/index.html");
}
};
}
答案 3 :(得分:0)
这个对我不起作用:
return "forward:/";
感谢Spring MVC @RestController and redirect我找到了一个很好的解决方案:
@RequestMapping(value = "/{[path:[^\\.]*}")
public void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("/");
}