문제
- React를 spring boot와 같이 war 파일로 말아 올렸을 때 문제 발생
원인
- 스프링 프로젝트는 구동하면 처음에 무조건 resources/static의 index.html 파일을 찾는다
- react는 index.html이 실행된 후 라우터가 해당 페이지에서 논리적으로 페이지를 로딩 => 계속해서 index.html을 사용하며 떠나지는 않는다
- 스프링 위에서 리액트를 실행했기 때문에 URL 매핑에서 이동하려는 페이지(html)를 찾아야 하는데 이게 없기 때문에 에러를 발생
해결
package 기존 프로젝트 패키지명;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class WebErrorController implements ErrorController {
@GetMapping({"/", "/error"})
public String index() {
return "index.html";
}
}
서버쪽 Controller에 위와 같은 WebErrorController를 생성해주면 해결
'React' 카테고리의 다른 글
[React, TypeScript] 실시간 시세표 일정 시간만 border 노출하기 (0) | 2023.09.08 |
---|---|
[React, TypeScript] Object is possibly 'null' (0) | 2023.09.07 |
[React, TypeScript] TradingView lightweight-charts 사용 (0) | 2023.09.07 |
[React] JS Bites: React hook is called in a function which is neither a React function or a custom React Hook (0) | 2023.09.07 |
[React] cannot read properties of undefined (reading '컬럼명') 오류 (0) | 2023.09.07 |