简体   繁体   English

如何使用springmvc-router配置spring-boot以提供静态文件

[英]How to configure spring-boot to serve static files using springmvc-router

I'm quite new to spring-mvc, so please let me know if I'm using it mistakenly or if there something I'm misunderstanding. 我是spring-mvc的新手,所以如果我使用错误或有误解,请告诉我。

I cannot serve static files on a spring-boot project. 我无法在spring-boot项目上提供静态文件。 There is no problem with the views generated by my controllers. 我的控制器生成的视图没有问题。

My spring-boot project is using springmvc-router to map the urls for the controllers in a single file instead of using @RequestMapping on each controller. 我的spring-boot项目使用springmvc-router在单个文件中映射控制器的URL,而不是在每个控制器上使用@RequestMapping

According to the documentation, I have to add the Router to my Spring MVC configuration; 根据文档,我必须将路由器添加到我的Spring MVC配置中。 in my case, I need to extend the RouterConfigurationSupport class. 就我而言,我需要扩展RouterConfigurationSupport类。 When I put this configuration (just like it is in the documentation) it causes the requests of static files to fail (it gives me the "white label error" and 404 in the log). 当我放置此配置时(就像在文档中一样),它将导致对静态文件的请求失败(在日志中显示“白色标签错误”和404)。

This is the configuration 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;
    }
}

I've found that RouterConfigurationSupport class extends (in some way) WebMvcAutoConfiguration . 我发现, RouterConfigurationSupport类扩展(在某种程度上) WebMvcAutoConfiguration

In the browser, the message displayed is: 在浏览器中,显示的消息是:

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

In the logs, it prints: 在日志中,它打印:

o.s.web.servlet.PageNotFound    : No mapping found for HTTP request with URI [/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcherServlet'

The springmvc-router works well for the views of my controllers, but now I cannot serve static files (like css or js). springmvc-router可以很好地用于我的控制器的视图,但是现在我无法提供静态文件(如CSS或JS)。

I have the notion that it is possible to 'manually' configure the static file resolution, but I'd prefer to take advantage of the spring-boot auto configuration. 我的想法是可以“手动”配置静态文件分辨率,但是我更喜欢利用spring-boot自动配置。

I've noticed that if I skip the configuration class mentioned above, the spring-boot auto configuration works normally and the static files get served, but then spring cannot find the routes for my controllers (obviously). 我已经注意到,如果我跳过上述配置类,则spring-boot自动配置可以正常工作,并且可以提供静态文件,但是spring显然找不到控制器的路由。


This is similar to this question Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration? 这与此问题类似。 是否可以扩展WebMvcConfigurationSupport并使用WebMvcAutoConfiguration? since I'm trying to add specific custom configuration but keeping the other configuration as default. 因为我尝试添加特定的自定义配置,但将其他配置保留为默认设置。

So I tried using a BeanPostProcessor like in this answer https://stackoverflow.com/a/28594582 and another similar in this issue's comment https://github.com/spring-projects/spring-boot/issues/5004#issuecomment-173714159 but I still cannot load static files. 所以我尝试使用像这个答案中的BeanPostProcessor https://stackoverflow.com/a/28594582,以及与此问题的评论中类似的另一个https://github.com/spring-projects/spring-boot/issues/5004#issuecomment- 173714159,但我仍然无法加载静态文件。

Another suggesion I've read was to copy the code from WebMvcAutoConfiguration class and append it to my specific configuration, but I don't really like this solution. 我读过的另一项建议是复制WebMvcAutoConfiguration类中的代码,并将其附加到我的特定配置中,但是我不太喜欢这种解决方案。

It might be not relevant, but I'm using a library for working with Jade as the templates' language. 可能无关紧要,但是我正在使用一个库来将Jade作为模板的语言使用。


  • Although I have already found a fix to this, I feel that I need to post this question because I found no other similar cases and because maybe there is another solution, hopefully better than mine. 尽管我已经找到了解决方法,但是我觉得我需要发布此问题,因为我没有发现其他类似的案例,并且也许还有其他解决方案,希望比我的更好。

I've found a way to solve the problem. 我找到了解决问题的方法。

Just like this answer https://stackoverflow.com/a/35482066/2363482 就像这个答案https://stackoverflow.com/a/35482066/2363482

I extended WebMvcAutoConfigurationAdapter with an empty class and loaded it using the @Import annotation in my configuration class. 我用一个空类扩展了WebMvcAutoConfigurationAdapter ,并在配置类中使用@Import批注将其加载。

Nevertheless, I'm not completely satisfied because according to the configuration example of springmvc-router, I should not use the @EnableWebMvc annotation but in the mentioned answer they use it and I'm using it too. 不过,我并不完全满意,因为根据springmvc-router的配置示例,我不应该使用@EnableWebMvc注释,但是在提到的答案中他们使用了它,我也正在使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM