cerbin
cerbin

Reputation: 1189

Spring static content not found

I am trying to use resources using Spring MVC. I have a class WebApplicationContextConfig that extends WebMvcConfigurerAdapter and in there I am overriding method addResourceHandlers:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**")
                .addResourceLocations("/resources/");
    }

In folder resources I have three images. But when I go in browser to get some image, for ex: http://localhost:8080/img/P123.jpeg I got 404 error.

enter image description here

Am I missing something?

Upvotes: 0

Views: 398

Answers (1)

biiyamn
biiyamn

Reputation: 506

  • you map an external URI to the physical path where resources are located so a request start with /img is mapped to physical path /resources under the web root application (webapp/resources) so your request should be http://localhost:8080/img/P123.jpeg
  • your request should start with your servlet mapping: if your servelt mapping is my-app your request should be http://localhost:8080/my-app/img/P123.jpeg
  • check that your resources folder is under webapp folder

Upvotes: 1

Related Questions