Created
February 28, 2023 22:09
-
-
Save gadmel/2634446a44d0eacc584fcac056902588 to your computer and use it in GitHub Desktop.
StaticFilesConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.backend.config; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.io.ClassPathResource; | |
import org.springframework.core.io.Resource; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
import org.springframework.web.servlet.resource.PathResourceResolver; | |
import java.io.IOException; | |
@Configuration | |
public class StaticFilesConfig implements WebMvcConfigurer { | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
registry.addResourceHandler("/**") | |
.addResourceLocations("classpath:/static/") | |
.resourceChain(true) | |
.addResolver(new PathResourceResolver() { | |
@Override | |
protected Resource getResource( | |
String resourcePath, | |
Resource location | |
) throws IOException { | |
Resource requestedResource = location.createRelative( | |
resourcePath | |
); | |
return ( | |
requestedResource.exists() && | |
requestedResource.isReadable() | |
) ? requestedResource | |
: new ClassPathResource("/static/index.html"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment