HttpServletRequest - sendError()
2024. 8. 21. 23:11ㆍSPRING/접했던 오류
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String requestURI = req.getRequestURI();
ControllerV2 controller = controllerMap.get(requestURI);
if (controller == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
MyView view = controller.process(req, resp);
view.render(req, resp);
}
상황
이상한 uri로 요청이 들어왔을 때 해당 요청을 처리할 객체가 없음을 바로 알리기 위해 sendError() 사용.
그러나,
500 server 에러와 함께 에러 페이지로 이동됨.
해결
return; 으로 메서드 종료 시켰더니 해결됨.
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String requestURI = req.getRequestURI();
ControllerV2 controller = controllerMap.get(requestURI);
if (controller == null) {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
MyView view = controller.process(req, resp);
view.render(req, resp);
}
원인
문제가 된 코드(최상단)는 찾는 controller가 null 이면 에러를 보내지만, 결국 아래 코드들이 실행됨.
controller 는 null 이므로, NPE가 터지면서, NPE가 터졌을 때의 후속처리(페이지 이동, 오류메세지 노출 등)가 없어
갈 곳 잃은 NPE가 클라이언트(브라우저)까지 돌아가며 500 에러 발생
java.lang.NullPointerException
====
예외 처리에 대한 학습 심화 필요
'SPRING > 접했던 오류' 카테고리의 다른 글
| Github에서 repo 생성할 때 Read.Me 추가만 했을 뿐인데 (1) | 2024.10.16 |
|---|---|
| jsp - 올바른 경로의 jsp를 찾지 못함. (0) | 2024.08.21 |
| H2 db - 예약어 (0) | 2024.08.07 |
| @RequestBody (0) | 2024.08.06 |
| @RequestParam-템플릿 에러 (0) | 2024.08.04 |