I have an spring boot app, which contains an angular front
like this:
src/main/resources/static/zanori2
Where in zanori2 I have the result of ng build some like:
index.html, index.js, favico.ico and so on
I tried this resourceHandle:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
/*@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {*/
//registry.addResourceHandler("/**/*")
/*.addResourceLocations("classpath:/static/zanori2/")
.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/zanori2/index.html");
}
});
}
}
However when I go to: localhost:8080/zanori2/index.html it return me to localhost:8080 and the js files works.
However it is weird because I am not allowed to share the url because if I go directly to localhost:8080 I get a not found page.
And with this other configuration:
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private Environment env;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/* Caching strategy */
boolean prodMode = Arrays.asList(env.getActiveProfiles()).contains("pro");
Integer cachePeriod = prodMode ? null : 0;
boolean useResourceCache = prodMode;
VersionResourceResolver versionResourceResolver = new VersionResourceResolver();
versionResourceResolver.addContentVersionStrategy("/**/*.js", "/**/*.css");
AppCacheManifestTransformer transformer = new AppCacheManifestTransformer();
/* robots.txt */
registry.addResourceHandler("/robots.txt")
.addResourceLocations("classpath:/static/robots.txt");
/* All other resources */
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/zanori2")
.setCachePeriod(cachePeriod)
.resourceChain(useResourceCache)
.addResolver(versionResourceResolver)
.addTransformer(transformer);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
/* Make sure Thymeleaf views are not accessible directly as static resources */
registry.addRedirectViewController("/app/*.html", "/");
/* Default mapping */
registry.addRedirectViewController("/", "/app/index.html");
/* Application entry */
registry.addViewController("/app/index.html").setViewName("index");
}
}
I go to localhost:8080/zanori2/index.html and I keep in the same url however my js files are not found so is not working too.
I do not found any example of this working properly.
