1

I am newbie to Springboot and I am trying to display html page on its root(localhost:8080) path. To do so I googled and gone through -

  1. Spring Boot not serving static content
  2. https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
  3. Spring Boot app not serving static content
  4. Springboot does not serve static content

Tried almost everything but none of them worked for me.

Exact problem

With out index.html file inside any of resources/(static/ or public/ or meta-inf/resources) works fine and show the list of some spring data rest. If I create an index.html file then it gives an error of 404 not found with out @EnableWebMvc annotation, if use @EnableWebMvc then it shows the list of Spring data rest apis.

Other than index.html file it show the list of Spring data api in root path, and url(localhost:8080/test.html) to other than index.html has same problem.

This problem has no effects by implementing public class StaticResourceConfiguration implements WebMvcConfigurer with this configuration to.

2
  • See this answer -stackoverflow.com/a/53698376/7849549 Commented Apr 23, 2020 at 19:14
  • @Shubh did not work for me, thank you for the link .. I missed that point Commented Apr 23, 2020 at 19:37

2 Answers 2

8

Starting with a simple spring boot initializer

... we can place static (html) files into one of:

  • (src/main/resources:)
  • static
  • resources
  • public
  • META-INF
    • resources

which results in the default (class path) locations, configured via the spring.resources.static-locations property.

These will be exposed through the value of spring.mvc.static-path-pattern-property (ref), by default: /**.

So a static index.html file in one of the above mentioned folders, with default config, will be accessible at:

Accordingly: no problem with http://localhost:8080/test.html ...


Checkout at github.

So this, at least answers the "question title" "how to serve static content in springboot 2.2.6?".


The order of spring.resources.static-locations appears (index.html preferred from META-INF/resources) also to be the "precedence" of static file locations (left-to-right, first match wins).


When we add @EnableWebMvc

..."evertyhing gets broken" (context loads, but) only:

WARN ... o.s.web.servlet.PageNotFound : No mapping for GET / WARN ... o.s.web.servlet.PageNotFound : No mapping for GET /index.html WARN ... o.s.web.servlet.PageNotFound : No mapping for GET /test.html 

..please aslo consider this: why spring-boot application doesn't require @EnableWebMvc

With "non-default config", You would have to provide more details, to find a specific solution.

But for "newbie in Springboot": starting with an intializer and "the defaults" sounds optimal! From here on, you can re-fine your configuration based on a working one.


And if you want/need the @EnableWebMvc annotation for some reason, this will result in the "previous" behavior again/restore the (2.2.6) default static content handling:

@EnableWebMvc @SpringBootApplication public class DemoApplication implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 

(Assuming no conflicts with existing configuration/resource handlers)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip, actually there was an issue with OpenApi jar file github.com/springdoc/springdoc-openapi/issues/361 (I was following old tutorial), Implementing new dependency solved the problem. Your statement about this should work helped alot and started to remove dependency and check the error one by one.
0

It's working for me

registry.addResourceHandler("//**").addResourceLocations("classpath:/static/"); 

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.