Use Spring Boot resource mapping together if a global request mapping

I’m currently trying to set up a Spring Boot MVC app together with React JS. For this reason I’ve added an easy frontend controller which listens to ** and serves the index file (builded react js html file).

That’s the code:

@Controller
public class FrontendController {

    @GetMapping("**")
    public String index() {
        return "index";
    }
}

In my webconfig class I have the following resource handlers to make sure all js, img and css files which are accessed by the react js html file through /static/...:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(
            "/static/**",
            "/favicon.ico",
            "manifest.json"
        )
            .addResourceLocations(
                "classpath:/static/static/",
                "classpath:/static/favicon.ico",
                "classpath:/static/manifest.json"
            );
    }

The problem is now that the GET mapping of my frontend controller is overwriting my resource handler. So that if I try to access a resource file like the main JS file (/static/js/main.js) the index html document will be served.

I’m not very experienced in Spring Boot and how these components work together but is there a way to prioritize the resources? Or how can I achieve that if the resource will be server in case of a match with /static/....

Thank you very much for your help!

49 thoughts on “Use Spring Boot resource mapping together if a global request mapping”

Leave a Comment