You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更新Packet对象时遇Request method 'GET' not supported错误,但数据已更新

问题分析与解决方案

嘿,我来帮你捋清楚这个问题——你遇到的「Request method 'GET' not supported」提示,其实是重定向环节的路径不匹配导致的,不过更新操作本身已经成功执行了,所以刷新页面后能看到修改后的Packet对象。

问题根源

你的POST处理方法modifyPacketPost执行完更新后,返回的是:

return "redirect:/packet/modify";

但你的GET请求映射是@GetMapping("/modify/{id}"),这个接口要求必须携带id参数才能访问修改页面。当POST完成后重定向到不带id的/packet/modify路径时,Spring找不到对应的GET处理方法,就抛出了「GET方法不支持」的错误。而此时packetService.updatePacket(packetDto)已经执行完毕,所以刷新页面(手动访问带id的路径)能看到更新后的结果。

修复方案

只需要修改POST方法的重定向路径,带上当前修改的Packet的id,让重定向后的请求能正确匹配到你的GET映射即可:

修改后的Controller代码:

@Controller
@RequestMapping("/packet")
public class PacketController {
    @GetMapping("/modify/{id}")
    public String modifyPacketGet(Model model, @PathVariable Long id) {
        model.addAttribute("channels", channelService.getAllChannels());
        model.addAttribute("packet", packetService.getById(id));
        return "packet/modify";
    }
    
    @PostMapping("/modify")
    public String modifyPacketPost(Model model, @ModelAttribute PacketDto packetDto) {
        packetService.updatePacket(packetDto);
        // 重定向时带上packet的id,匹配带参数的GET映射
        return "redirect:/packet/modify/" + packetDto.getId();
    }
}

额外优化建议(可选)

你也可以让POST映射的路径和GET保持统一,比如把POST方法的注解改成@PostMapping("/modify/{id}"),同时修改HTML表单的action路径:

<form th:action="@{/packet/modify/{id}(id=${packet.id})}" method="post" th:object="${packet}" enctype="multipart/form-data">

这样路径结构更清晰,但核心的修复还是重定向时必须携带id参数。

内容的提问来源于stack exchange,提问作者user11042713

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:24:41