... we can place static (html) files into one of:
- (src/main/resources:)
- static
- resources
- public
- META-INF
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)