0

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?

1 Answer 1

1
  • 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
Sign up to request clarification or add additional context in comments.

6 Comments

I made a mistake in a url path, of course it was the one that you wrote. However, what do you mean by saying webapp folder? I am placing all images in src/main/resources, should I move it somewhere else?
Ok so everything is clear now, I had my image in "main" resources folder, not in webapp resources folder, when I moved images to /webapp/resources/ everything is working fine.
if you put images in src/main/resources the location should be .addResourceLocations("classpath:/");
So "/" means main project "resources" directory? And if for ex. I create directory "images" in "resources", when adding resource location I should write "/resources/images/"?
"classpath:/images/" instead
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.