如何配置 spring-boot 以使用 springmvc-router 提供静态文件
How to configure spring-boot to serve static files using springmvc-router
我是 spring-mvc 的新手,所以如果我用错了或者有什么误解,请告诉我。
我无法在 spring-boot 项目上提供静态文件。我的控制器生成的视图没有问题。
我的 spring-boot
项目使用 springmvc-router
在单个文件中映射控制器的 url而不是在每个控制器上使用 @RequestMapping
。
根据文档,我必须将路由器添加到我的 Spring MVC 配置中;就我而言,我需要扩展 RouterConfigurationSupport
class。当我放置此配置(就像在文档中一样)时,它会导致静态文件的请求失败(它在日志中给了我 "white label error" 和 404)。
这是配置class:
import org.resthub.web.springmvc.router.RouterConfigurationSupport;
@Configuration
@ComponentScan(basePackages = "my.project.demo.controllers")
// You should not use the @EnableWebMvc annotation
public class WebAppConfig extends RouterConfigurationSupport {
@Override
public List<String> listRouteFiles() {
List<String> routeFiles = new ArrayList<String>();
routeFiles.add("classpath:routes.conf");
return routeFiles;
}
}
我发现 RouterConfigurationSupport
class 扩展(以某种方式)WebMvcAutoConfiguration
。
在浏览器中,显示的消息是:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Apr 28 11:43:10 CDT 2016
There was an unexpected error (type=Not Found, status=404).
No message available
在日志中,它打印:
o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcherServlet'
springmvc-router 对我的控制器的视图运行良好,但现在我无法提供静态文件(如 css 或 js)。
我认为可以 'manually' 配置静态文件分辨率,但我更愿意利用 spring-boot 自动配置。
我注意到,如果我跳过上面提到的配置 class,spring-boot 自动配置可以正常工作,并且可以提供静态文件,但是 spring 不能找到我的控制器的路线(显然)。
这类似于这个问题Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?,因为我正在尝试添加特定的自定义配置,但将其他配置保留为默认配置。
所以我尝试像这个答案一样使用 BeanPostProcessor
和本期评论中的另一个类似
https://github.com/spring-projects/spring-boot/issues/5004#issuecomment-173714159 但我仍然无法加载静态文件。
我读到的另一个建议是从 WebMvcAutoConfiguration class 复制代码并将其附加到我的特定配置,但我不太喜欢这个解决方案。
它可能不相关,但我正在使用一个库来使用 Jade 作为模板语言。
- 虽然我已经找到了解决办法,但我觉得我需要 post 这个问题,因为我没有发现其他类似的情况,因为也许还有另一种解决方案,希望更好比我的还多
我找到了解决问题的方法。
就像这个答案
我用一个空 class 扩展了 WebMvcAutoConfigurationAdapter
并在我的配置 class.
中使用 @Import
注释加载它
尽管如此,我并不完全满意,因为根据 springmvc-router 的配置示例,我不应该使用 @EnableWebMvc
注释,但在提到的答案中他们使用它,我也在使用它.
我是 spring-mvc 的新手,所以如果我用错了或者有什么误解,请告诉我。
我无法在 spring-boot 项目上提供静态文件。我的控制器生成的视图没有问题。
我的 spring-boot
项目使用 springmvc-router
在单个文件中映射控制器的 url而不是在每个控制器上使用 @RequestMapping
。
根据文档,我必须将路由器添加到我的 Spring MVC 配置中;就我而言,我需要扩展 RouterConfigurationSupport
class。当我放置此配置(就像在文档中一样)时,它会导致静态文件的请求失败(它在日志中给了我 "white label error" 和 404)。
这是配置class:
import org.resthub.web.springmvc.router.RouterConfigurationSupport;
@Configuration
@ComponentScan(basePackages = "my.project.demo.controllers")
// You should not use the @EnableWebMvc annotation
public class WebAppConfig extends RouterConfigurationSupport {
@Override
public List<String> listRouteFiles() {
List<String> routeFiles = new ArrayList<String>();
routeFiles.add("classpath:routes.conf");
return routeFiles;
}
}
我发现 RouterConfigurationSupport
class 扩展(以某种方式)WebMvcAutoConfiguration
。
在浏览器中,显示的消息是:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Apr 28 11:43:10 CDT 2016
There was an unexpected error (type=Not Found, status=404).
No message available
在日志中,它打印:
o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcherServlet'
springmvc-router 对我的控制器的视图运行良好,但现在我无法提供静态文件(如 css 或 js)。
我认为可以 'manually' 配置静态文件分辨率,但我更愿意利用 spring-boot 自动配置。
我注意到,如果我跳过上面提到的配置 class,spring-boot 自动配置可以正常工作,并且可以提供静态文件,但是 spring 不能找到我的控制器的路线(显然)。
这类似于这个问题Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?,因为我正在尝试添加特定的自定义配置,但将其他配置保留为默认配置。
所以我尝试像这个答案一样使用 BeanPostProcessor 和本期评论中的另一个类似 https://github.com/spring-projects/spring-boot/issues/5004#issuecomment-173714159 但我仍然无法加载静态文件。
我读到的另一个建议是从 WebMvcAutoConfiguration class 复制代码并将其附加到我的特定配置,但我不太喜欢这个解决方案。
它可能不相关,但我正在使用一个库来使用 Jade 作为模板语言。
- 虽然我已经找到了解决办法,但我觉得我需要 post 这个问题,因为我没有发现其他类似的情况,因为也许还有另一种解决方案,希望更好比我的还多
我找到了解决问题的方法。
就像这个答案
我用一个空 class 扩展了 WebMvcAutoConfigurationAdapter
并在我的配置 class.
@Import
注释加载它
尽管如此,我并不完全满意,因为根据 springmvc-router 的配置示例,我不应该使用 @EnableWebMvc
注释,但在提到的答案中他们使用它,我也在使用它.