Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring serving static contents with dot(s) in filename

I would like to serve via Spring the web pages resulting from a build in npm and everything is working fine but I could not serve resources with name like main.xxxx.yyy no matter what the real suffix is (css, js or html).

the directory tree is like that:

src/main/resource/resource
                  index.html
                  asset-manifest.json
                  favicon.ico
                  manifest.json
                  service-worker.js
                  static
                     css
                         main.fc656101.css
                         main.fc656101.css.map
                     js
                         main.91794276.js
                         main.91794276.js.map
                     media
                         banner.bdcf92f4.jpg
                         fontawesome-webfont.912ec66d.svg
                         ...

This is the application class:

@SpringBootApplication
public class Application {
   private static Logger log=Logger.getLogger(Application.class.getName());

@Bean
WebMvcConfigurer configurer () {
    return new WebMvcConfigurerAdapter() {

        @Override
        public void addResourceHandlers (ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/static/*").
                      addResourceLocations("classpath:/static/");
        }
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            super.configurePathMatch(configurer);

            configurer.setUseSuffixPatternMatch(false);
        }
    };
}

public static void main(String[] args) {

    SpringApplication.run(Application.class, args);
}

To debug the issue I've manually renamed some files and it works, so I've restricted the problem to the file names having dot(s) in it.

I've saw that someone has solved a similar problem adding {variable:.+} in request mapping in RestControllers, but I don't have controllers, so I could not figure out how to do it.

EDIT:

I've found that with this configuration:

@Configuration
class ServletConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configurePathMatch(final PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
        configurer.setUseTrailingSlashMatch(false);
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }

}

Now it serves all *.html, included page.01.html, but still not style.01.css or script.01.js. I assume is a different problem and the original one is solved by ContentNegotiationConfigurer.

like image 792
Marcoc1712 Avatar asked Dec 08 '17 21:12

Marcoc1712


People also ask

How static files are served in spring?

Using Spring BootSpring Boot comes with a pre-configured implementation of ResourceHttpRequestHandler to facilitate serving static resources. By default, this handler serves static content from any of the /static, /public, /resources, and /META-INF/resources directories that are on the classpath.

How do I register a static assets folder in spring boot?

Step 1: Move images folder from src/main/resources/static/images to src/main/webapp/WEB-INF/images. Step 2: Look up SpringBootMainApplication. java add code @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // Register resource handler for images registry.

Where do static files go in spring boot?

The file is located in the src/main/resources/static directory, which is a default directory where Spring looks for static content. In the link tag we refer to the main. css static resource, which is located in the src/main/resources/static/css directory. In the main.


1 Answers

I wrote this should be a very stupid question...

Problem was browser cache and project cleaning. Be sure to ALWAYS clear the cache (that's quite obvious) but also clean the project from where you serve the static contents after changing configuration. Stop and restart JAVA is not enought.

That costed to me three days but now is working, and the correct configuration is the first I've posted, no needs for contentNegotiation configuration.

Hope this could save a day for others!

like image 64
Marcoc1712 Avatar answered Oct 25 '22 09:10

Marcoc1712