Thursday, April 16, 2020

Can't serve static html with Spring Boot 2

Solution next error.

Add next class:

package your_package;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class StaticResourceConfiguration implements WebMvcConfigurer {

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
  "classpath:/META-INF/resources/",
  "classpath:/resources/",
  "classpath:/static/",
  "classpath:/public/"
 };

 @Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
  registry.addResourceHandler("/**")
   .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
 }

}


and try again access, for example:

http:localhost:8080/your_html.html

or

http:localhost:8080/your_context/your_html.html













References:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot







No comments:

Post a Comment

Provisioning Cloud SQL with Private Service Connect Using Terraform & Accessing from Cloud Run with Spring Boot

In this post, we'll explore how to provision Cloud SQL instances with Private Service Connect (PSC) connectivity using Terraform and the...