How to use React's BrowserRouter on client and Java REST API (Spring Boot) on the server?

Karl.S :

I want to use React on the client and Java Spring Boot on the server for REST API, both client and server are packaged together so they are hosted on the same application server.

I can already query the server APIs via the /api path.

But (how) can I use the BrowserRouter of react-router-dom (Router v4) on the client instead of the HashRouter?

I don't know Spring Boot well, but I guess I could check on the server if the route does not match /api, then I would return the index.html with all the react logic handling the route based on HTTP query location path?

I hope I am clear, I don't want the hash in the URL and routing must be done on the client.

Karl.S :

I finally found how to catch all GET requests and return my React app, it was deadly simple, just use a wild card *, note that /* will not work.

@GetMapping("*")
public ModelAndView defaultPage(Map<String, Object> model) {
    model.put("contextPath", contextPath);
    return new ModelAndView("index", model);
}

In this example, the index points to this file /src/main/resources/static/index.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463311&siteId=1