0

I am attempting to configure my Spring Boot MVC application to serve some audio files under static\recordings in my resources directory.

I've added the following mapping

public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/recordings/**.wav", "/recordings/**.mp3").addResourceLocations("/static/recordings/");
}

However, requesting either:

/recordings/general/en/en-general-choose-language.wav
static/recordings/general/en/en-general-choose-language.wav

Gives a 404, with this in the logs (adjusted slightly based on path, of course):

Looking up handler method for path /recordings/general/en/en-general-choose-language.wav
Did not find handler method for [/recordings/general/en/en-general-choose-language.wav]

I have a work-around with a Controller and writing the file contents to the response stream but it seems silly to have to do that to serve a static file.

ThisIsNoZaku
  • 2,213
  • 2
  • 26
  • 37
  • Can you try `public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/resources/**") .addResourceLocations("/resources/"); }` in your config class? – harshavmb Jun 29 '17 at 17:58
  • If you are using springboot, then you can set `spring.resources.static-locations` – harshavmb Jun 29 '17 at 18:00
  • Try to read about how to serve static content with Spring boot, here's one of my posts: https://stackoverflow.com/questions/41057042/spring-boot-css-showing-up-blank-not-loading-after-trying-everything/41058923#41058923 – Moshe Arad Jun 29 '17 at 19:02

1 Answers1

0

The documentation of ResourceHandlerRegistry#addResourceHandler states the following: http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.html#addResourceHandler-java.lang.String...-

Patterns like "/static/**" or "/css/{filename:\w+\.css}" are allowed. See AntPathMatcher for more details on the syntax.

http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html

In the description of AntPathMatcher we find an example matching your needs:

org/springframework/**/*.jsp — matches all .jsp files underneath the org/springframework path

Hence, try "/recordings/**/*.wav" as the handler pattern.

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30