DMcg
DMcg

Reputation: 129

Serving single html page with Spring Boot

I'm developing a simple Spring Boot app that uses Spring Boot Web. I placed my index.html into the templates subdirectory of the resources directory as per the Spring Boot docs. I've defined inside the application.properties file:

spring.mvc.view.prefix=classpath:/templates/

spring.mvc.view.suffix=.html

My WebMvcConfigurerAdapter looks like this:

@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("index");
    super.addViewControllers(registry);
}   
}

My problem is I cant seem to serve index.html at http://localhost:8080. I get the following error: "javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'".

How can I fix this without the use of a controller?

Kind regards, Dave

Upvotes: 0

Views: 925

Answers (1)

Maciej Walkowiak
Maciej Walkowiak

Reputation: 12932

In order to serve static content from Spring Boot application you just need to place them in src/main/resources/static - no other configuration needed.

  • put index.html to src/main/resources/static/index.html
  • delete properties from application.properties
  • delete addViewControllers method and @EnableWebMvc annotation - you can actually delete whole class
  • access index by going to http://localhost:8080/

Upvotes: 1

Related Questions