React Router 与 Spring 引导路由冲突
React Router conflicts with Spring Boot routing
我正在创建 Spring 在前端使用 React 的启动应用程序。我可以在浏览器中打开页面,但在后端出现错误:
RequestRejectedException: The request was rejected because the URL contained a potentially malicious String "//"
所以我有一个简单的java视图控制器:
@Controller
public class ViewController {
@RequestMapping("/")
public String index() {
return "index";
}
}
它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>
所以在 React 应用程序中,我连接了 react-router,这是我的 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;
所以基本上我可以访问主页,它呈现正常。我可以在路由之间切换,但同时在后端我看到错误。我调试并意识到为什么会这样。主页的url是
http://localhost:8080/#/
如果我用url http://localhost:8080/#
没问题,没有例外。我怎样才能解决这个问题?如果我删除其中一个斜杠将无法正常工作
在构建单页应用时,您应该在后端使用 @RequestMapping("/**")
,否则如果您尝试访问与主页不同的页面,您将收到 404。
我正在创建 Spring 在前端使用 React 的启动应用程序。我可以在浏览器中打开页面,但在后端出现错误:
RequestRejectedException: The request was rejected because the URL contained a potentially malicious String "//"
所以我有一个简单的java视图控制器:
@Controller
public class ViewController {
@RequestMapping("/")
public String index() {
return "index";
}
}
它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>
所以在 React 应用程序中,我连接了 react-router,这是我的 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;
所以基本上我可以访问主页,它呈现正常。我可以在路由之间切换,但同时在后端我看到错误。我调试并意识到为什么会这样。主页的url是
http://localhost:8080/#/
如果我用url http://localhost:8080/#
没问题,没有例外。我怎样才能解决这个问题?如果我删除其中一个斜杠将无法正常工作
在构建单页应用时,您应该在后端使用 @RequestMapping("/**")
,否则如果您尝试访问与主页不同的页面,您将收到 404。