0

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.

I cannot serve static files on a spring-boot project. 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.

According to the documentation, I have to add the Router to my Spring MVC configuration; in my case, I need to extend the RouterConfigurationSupport class. 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).

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.

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).

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.

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).


This is similar to this question Is it possible to extend WebMvcConfigurationSupport and use 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.

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.

It might be not relevant, but I'm using a library for working with Jade as the templates' language.


  • 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.
Community
  • 1
  • 1
raafaar
  • 108
  • 7

1 Answers1

0

I've found a way to solve the problem.

Just like this answer https://stackoverflow.com/a/35482066/2363482

I extended WebMvcAutoConfigurationAdapter with an empty class and loaded it using the @Import annotation in my configuration class.

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.

Community
  • 1
  • 1
raafaar
  • 108
  • 7