patch 方法不支持 post 方法

发布于 2025-01-20 06:55:19 字数 3572 浏览 0 评论 0原文

我希望通过Hibernate-validator验证编辑实体,但是在调用补丁方法时,会抛出错误:不支持的邮政方法。如何使@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>

I want edit entity with validating by hibernate-validator, but when calling the patch method, an error is thrown: post method not supported. How to make @PatchMapping work correctly? I am a beginner developer, help me please.

Controller:

    @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";
    }

Service:

    @Transactional
    public void update(Project project){
        this.projectRepository.update(project.getId(),
                                    project.getTitle(),
                                    project.getDescription());
    }

Repository:

@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:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和我恋爱吧 2025-01-27 06:55:19

您无法将“补丁”用于表单方法属性。只允许使用“ get”和“ post”方法(来源)。

you can not use 'PATCH' for the form method attribute. only 'GET' and 'POST' methods are allowed(source).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文