2

Files in resources/static are not available. How I can fix it?

Hierarchy:

resources/ db/ static/ image.png templates/ application.properties 

But if I open localhost:8081/image.png I get error.

My WebConfig:

@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/webjars/**") .addResourceLocations("/webjars/"); } } 
4
  • how do you run it? Commented Aug 16, 2018 at 9:13
  • What is .addResourceLocations("/webjars/");? Commented Aug 16, 2018 at 9:24
  • @Adya I use bootstrap in some templates. <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.7</version> </dependency> Commented Aug 16, 2018 at 9:28
  • @diginoise docker-compose up Commented Aug 16, 2018 at 9:29

3 Answers 3

2

Here give path of your image:

registry.addResourceHandler("/**").addResourceLocations("file:/path/to/your/image/"); 
Sign up to request clarification or add additional context in comments.

Comments

1

You should add below line to your existing resource mapping:

 registry.addResourceHandler("/**") .addResourceLocations("resources/static/"); 

You have configured the webjars resourceHandler to serve client side script or stylesheet dependencies. but custom handler is not added to serve your image file.

Since you are overriding the addResourceHandlers (ResourceHandlerRegistry registry) method, you should provide all the resourceLocation and handler mapping in your implementation.

Please check serving-static-web-content-with-spring-boot article to have more clear idea.

Note:

If you are using spring-boot you shouldn't be overriding the above method untill explicitly required as its already taken care in WebMvcAutoConfiguration.java.

Please check below default implementation:

@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { if (!this.resourceProperties.isAddMappings()) { logger.debug("Default resource handling disabled"); return; } Duration cachePeriod = this.resourceProperties.getCache().getPeriod(); CacheControl cacheControl = this.resourceProperties.getCache() .getCachecontrol().toHttpCacheControl(); if (!registry.hasMappingForPattern("/webjars/**")) { customizeResourceHandlerRegistration(registry .addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/") .setCachePeriod(getSeconds(cachePeriod)) .setCacheControl(cacheControl)); } String staticPathPattern = this.mvcProperties.getStaticPathPattern(); if (!registry.hasMappingForPattern(staticPathPattern)) { customizeResourceHandlerRegistration( registry.addResourceHandler(staticPathPattern) .addResourceLocations(getResourceLocations( this.resourceProperties.getStaticLocations())) .setCachePeriod(getSeconds(cachePeriod)) .setCacheControl(cacheControl)); } } 

Comments

0

If you would prefer to serve static content (including web pages, js, pdf, css, pdf, doc etc.) from outside of the WAR, may be that is usefull. if you wanna changed any that static contents, just you can put changed file to your content path on application server easly and serving that without deploying or restarting you application server. If you prefer this way, you can do it with a small configuration on your application server.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.