Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router conflicts with Spring Boot routing

I'm creating Spring Boot app using React on frontend side. I can open the page in a browser, but on backend side appears error:

RequestRejectedException: The request was rejected because the URL contained a potentially malicious String "//"

So I have a simple java-view controller:

@Controller
public class ViewController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }
}

It returns html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" http-equiv="CONTENT-TYPE" content="text/html; charset=UTF-8"/>
    <title>Hello demo</title>
<link href="/build/react_app.css" rel="stylesheet"></head>
<body>
<div id="react"></div>
<script src="/build/js/main.min.js"></script><script src="/build/js/react_app.min.js"></script></body>
</html>

So in react app I connected react-router and here is my index.js:

const middleware = [thunk, createLogger()];
let container = document.getElementById('react');

const store = createStore(
    reducer,
    compose(applyMiddleware(...middleware))
);

ReactDOM.render(
    <Provider store={store}>
        <Router history={hashHistory}>
            <Switch>
                <Route exact path="/" component={MainPage}/>
                <Route exact path="/login" component={LoginPage}/>
                <Route exact path="/registration" component={RegistryPage}/>
                <Route path="*" component={NotFound}/>
            </Switch>
        </Router>
    </Provider>,
    container
);

export default store;

So basically I can access main page, it renders ok. I can switch between routes, but in the same time on backend I see error. I debugged and realized why it happens. url of main page is

http://localhost:8080/#/

If I use url http://localhost:8080/# it's ok, and no exceptions. How can I fix this? If I remove one of these slashes doesn't work properly

like image 956
Sam Fisher Avatar asked Sep 09 '26 00:09

Sam Fisher


1 Answers

As you are building a single page app, you should use @RequestMapping("/**") in the backend otherwise you will get a 404 if you try to reach a different page than the homepage.

like image 143
Olivier Boissé Avatar answered Sep 11 '26 19:09

Olivier Boissé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!