Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

java - 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!

question from:https://stackoverflow.com/questions/65928528/use-spring-boot-resource-mapping-together-if-a-global-request-mapping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

by using this

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

in your controller you're telling it that whatever the url is return index why not give the index some specific url like "/home"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...