Я хочу отредактировать объект с проверкой hibernate-validator, но при вызове метода patch выдается ошибка: post method not supported
. Как заставить @PatchMapping
работать правильно? Я начинающий разработчик, помогите пожалуйста.
Контроллер:
@GetMapping("/edit/{id}") //for admin & developer
public String edit(@PathVariable("id") Long id, Model model, @AuthenticationPrincipal UserDetails user){
final ProjectDTO project = this.projectService.getDTOById(id);
model.addAttribute("project",project);
model.addAttribute("currentUser", this.userService.findUserByNickname(user.getUsername()));
return "project/edit";
}
@PatchMapping("/update")
public String update(@RequestParam("project") @Valid Project project, BindingResult bindingResult,
Model model, @AuthenticationPrincipal UserDetails user,
@RequestParam("files") MultipartFile[] files) throws IOException{
final User author = userService.findUserByNickname(user.getUsername());
model.addAttribute("currentUser", author);
model.addAttribute("files", files);
if (bindingResult.hasErrors()){
return "project/edit";
}
List<FileInfo> infoList = this.projectService.upload(Arrays.asList(files));
project.setFileList(infoList);
project.setPreviewId(infoList.get(0).getId());
this.projectService.update(project);
return "redirect:/portfolio";
}
Услуга:
@Transactional
public void update(Project project){
this.projectRepository.update(project.getId(),
project.getTitle(),
project.getDescription());
}
Репозиторий:
@Transactional
@Modifying
@Query(value = "UPDATE Project p SET p.title = :title, p.description = :description WHERE p.id = :id")
void update(@Param("id") Long id,
@Param("title") String title,
@Param("description") String description);
HTML-форма:
<form th:action = "@{/portfolio/update}" th:method = "patch" th:object = "${project}" enctype = "multipart/form-data">
<div class = "card-subtitle my-3">
<input class = "form-control form-control-lg" type = "text" th:field = "*{title}" id = "title" th:value = "${project.getTitle()}">
<div style = "color:red" th:if = "${#fields.hasErrors('title')}" th:errors = "*{title}" class = "validation-message">TITLE_ERROR</div>
</div>
<div class = "card-text my-2">
<textarea placeholder = "Описание проекта" class = "form-control form-control-lg" type = "text" th:field = "*{description}" rows = "7" cols = "65" id = "description" th:inline = "text">
[[${project.getDescription}]]
</textarea>
<div style = "color:red" th:if = "${#fields.hasErrors('description')}" th:errors = "*{description}" class = "validation-message">DESCRIPTION_ERROR</div>
</div>
<input class = "my-3 form-control " type = "file" name = "files" multiple>
<div th:text = "${error}">FILE_ERR</div>
<div class = "card-footer d-flex justify-content-end">
<button type = "submit" class = "btn btn-success p-1 bd-highlight">Обновить проект</button>
</div>
</form>
вы не можете использовать «PATCH» для атрибута метода формы. разрешены только методы GET и POST (источник).
стандартный метод формы html может быть только получен или отправлен. если вы используете spring-boot 2.2 или выше, это можно включить с помощью свойства. проверить ответ здесь stackoverflow.com/questions/64881009/…