Reputation: 3579
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 -
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.
Upvotes: 1
Views: 9845
Reputation: 11
It's working for me
registry.addResourceHandler("//**").addResourceLocations("classpath:/static/");
Upvotes: 0
Reputation: 13261
... we can place static (html) files into one of:
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).
@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)
Upvotes: 8