I have Spring Boot 2.3.1 and React.js 16.3.1 with react router and I want to serve React single page app with Spring Boot.
It all works fine, until the path is changed by router and I refresh the page. Then the mapping returns index.html file correctly but other files (.js, .css, etc.) are not loaded. The problem is that it looks for them using wrong urls. For instance when I'm in path http://localhost:8080/a/b/c
then it tries to load other files from paths like these: http://localhost:8080/a/b/static/css/main.a907b239.chunk.css
or http://localhost:8080/a/b/favicon.ico
so the '/a/b' is the problem. When running app on node.js server it works fine.
This is my file structure, all react files are in the static folder: File structure
This is my controller to forward all requests to '/' path, so index.html is always returned:
@Controller
public class ForwardingController {
@RequestMapping("/**/{path:[^\\.]+}")
public String forward() {
return "forward:/";
}
}
Is there any way to make the app look for files using the correct urls or forward all these requests?
I have already tried this (https://stackoverflow.com/a/47704160):
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/{spring:\\w+}")
.setViewName("forward:/");
registry.addViewController("/**/{spring:\\w+}")
.setViewName("forward:/");
registry.addViewController("/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}")
.setViewName("forward:/");
}
}
But it basically works the same as my controller and doesn't solve the problem