簡體   English   中英

在反應中使用 Spring Boot & BrowserRouter 時出現 Whitelabel 錯誤頁面

[英]Whitelabel Error Page when using Spring Boot & BroweserRouter in react

我確實有一個使用 BrowserRouter 在頁面之間路由的反應應用程序。 我還有一個帶有 Spring 引導的 Java 后端。

當我使用 applicationRunner 和 npm 啟動后端和前端時,瀏覽器路由器運行良好。 例如 http://localhost:3000/home 工作正常。 並且 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>

但是當我嘗試與java -jar./bapplication-main/target/beregning-oversikt-main-0-SNAPSHOT.jar一起啟動服務器時,麻煩就開始了。

該應用程序現在在 localhost:8080 上運行,因此起始頁可以正常工作,但 localhost:8080/home 不再工作,但 localhost:8080/api/collection/{id} 仍然有效。

我的猜測是 Spring Boot 和 React 路由在一起有些麻煩,但我找不到答案

路線“/”可能在 reactjs 和 spring 引導中定義。

因此,在 spring 啟動 IndexController 中使用“/”刪除索引方法,需要刪除以下方法..它將適用於 React 主頁。

@Controller
public class IndexController {
    @GetMapping("/")
    public String index(Model model) {

        return "index";
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM