Error Note
create가 제대로 작동하지 않았던 이유
롱하
2024. 6. 25. 11:05
게시글을 저장했을때 create가 제대로 작동하지 않았다.
기존 코드
@PostMapping(path = "/problem/create")
String operate(RedirectAttributes redirectAttributes, Authentication authentication,
@Valid @ModelAttribute("dto") ProblemDto dto,
BindingResult bindingResult, Model model, HttpServletRequest request) throws IOException {
BranchDto branch = (BranchDto)request.getAttribute("commonCurrentBranch");
if(bindingResult.hasErrors()) {
commonUtil.addJsonAlertMessages(commonUtil.getRequest().getSession(), bindingResult);
return "branch/application/problem/view";
}
Problem createdItem = problemCreateUpdateService.operate(modelMapper.map(dto, Problem.class));
return "redirect:/problem/complete";
}
}
수정 코드
@PostMapping(path = "/problem/create")
String operate(RedirectAttributes redirectAttributes, Authentication authentication,
@Valid @ModelAttribute("dto") ProblemDto dto,
BindingResult bindingResult, Model model, HttpServletRequest request) throws IOException {
BranchDto branch = (BranchDto)request.getAttribute("commonCurrentBranch");
Problem createdItem = problemCreateUpdateService.operate(modelMapper.map(dto, Problem.class));
return "redirect:/problem/complete";
}
}
>> bindingResult.hasErrors() 이 부분만 제거 해줬더니 제대로 create 되었다.
저 에러내용은 무엇인지 제대로 파악한 후 근본적인 해결이 필요하다고 생각하였음
- 해결 -
유효성 검사 실패
Dto에 항목에 걸어두었던 '@NotNull ' 유효성 검사를 통과하지 못했기때문에 @Valid 어노테이션을 붙이므로 해서 유효성 검사 실패로 bindingResult.hasErrors()가 true를 반환하였던 것이다. 그래서 그 이후에 ' problemCreateUpdateService.operate '가 실행되지 않아서 데이터베이스에 데이터가 저장되지 않았던것
유효성 검사를 통과하게 Dto를 수정하거나 create 하기전에 데이터를 넣으면 된다.
Front에서 필요없던 @NotBlank를 Dto에서 제거해줌으로서 해결완료