Spring
Jar에서만 View의 경로를 찾지 못하는 이유
porongporo
2023. 3. 21. 23:11
간단한 스프링 어플리케이션을 작성하다가 생긴 문제점에 대해 공유하고자 합니다. Static 파일이 아닌 templates 파일에 대해서 컨트롤러에서 아래 예시처럼 경로 앞에 "/"가 포함되는지 여부에 따라 jar에서는 뷰를 찾지 못하는 404 에러가 발생했습니다.
@GetMapping("/users/list")
public String userList(Model model) {
model.addAttribute("user", joinService.lookupAllUser());
return "user/list"; // IDE + Jar에서 문제 없이 작동
}
}
@GetMapping("/users/list")
public String userList(Model model) {
model.addAttribute("user", joinService.lookupAllUser());
return "/user/list"; // IDE에서 작동되나 Jar에서 404 Error 발생
}
}
결론만 말하면 슬래시가 앞에 있는 경우에는 Jar 빌드 파일에서는 경로를 찾지 못합니다. Classpath가 mustache template engine을 사용한 경우 default가 classpath:/templates/로 되어있습니다. 이 경우 위 예시의 아래처럼 뷰를 반환하면
/test/test
classpath:/templates//test/test.html
slash가 중복되어 Mapping이 일어나지 않아 404 에러가 발생하는 것입니다. 다만 IDE에서 빌드 시 컴파일러가 이러한 경로의 오류를 잘 수정해준다고 이해할 수 있을 것 같습니다.