[英]Whitelabel Error Page when using Spring Boot & BroweserRouter in react
I do have a react-application using BrowserRouter for routing between pages.我确实有一个使用 BrowserRouter 在页面之间路由的反应应用程序。 I also have a Java Backend with Spring Boot.我还有一个带有 Spring 引导的 Java 后端。
When I start the backend and frontend seperataly with an applicationRunner and npm start the browserrouter works perfectly.当我使用 applicationRunner 和 npm 启动后端和前端时,浏览器路由器运行良好。 For example http://localhost:3000/home works fine.例如 http://localhost:3000/home 工作正常。 And also localhost:8080/api/collection/{id} works fine with this code并且 localhost:8080/api/collection/{id} 也适用于此代码
Java-backend:
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
public class ApiController {
@Autowired
private BeregningstjenestePoller poller;
@GetMapping("/collection/{id}")
public CollectionV2 withId(@PathVariable String id) {
return poller.getCollectionWithId(id);
}
React-frontend:
<BrowserRouter>
<Route exact path={'/'}>
<StartPage title={'Hello'} />
</Route>
<Route exact path={'/home'}>
<HomePage />
</Route>
</BrowserRouter>
But when I try to start the servers together with java -jar./bapplication-main/target/beregning-oversikt-main-0-SNAPSHOT.jar
the trouble starts.但是当我尝试与java -jar./bapplication-main/target/beregning-oversikt-main-0-SNAPSHOT.jar
一起启动服务器时,麻烦就开始了。
The application is now running on localhost:8080, so the starting page works, but localhost:8080/home doesnt work anymore, but localhost:8080/api/collection/{id} still works.该应用程序现在在 localhost:8080 上运行,因此起始页可以正常工作,但 localhost:8080/home 不再工作,但 localhost:8080/api/collection/{id} 仍然有效。
My guess is some trouble with Spring Boot and React routing together, but I cant find an answer我的猜测是 Spring Boot 和 React 路由在一起有些麻烦,但我找不到答案
Might the Route "/" is defined in both reactjs and spring boot.路线“/”可能在 reactjs 和 spring 引导中定义。
So remove index method with "/" in spring boot IndexController, below method need to be removed.. it will work with React home page.因此,在 spring 启动 IndexController 中使用“/”删除索引方法,需要删除以下方法..它将适用于 React 主页。
@Controller
public class IndexController {
@GetMapping("/")
public String index(Model model) {
return "index";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.