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

Spring Boot同Controller中ResponseEntity方法如何重定向到GetMapping接口

问题根因

跳转逻辑不生效是三个问题叠加导致的:

  1. 代码笔误:你接收业务层返回结果的变量名是inventoryCreationResponse,但后续取值判断时写的是limitCreationResponse,代码运行时会直接抛空指针异常,根本走不到跳转分支。
  2. 重定向路径错误:你写的跳转地址inventories没有加前导斜杠/,属于相对路径,302响应返回后浏览器会把路径拼接到当前POST请求的父路径下,比如当前请求是/create_inventory_record,最终跳转地址会变成错误的/create_inventory_record/inventories,匹配不到你定义的@GetMapping("/inventories")路由。
  3. 请求类型不匹配重定向逻辑:你的POST接口显式声明了produces = MediaType.APPLICATION_JSON_VALUE,如果前端是用axios/fetch发的异步AJAX请求,浏览器不会自动执行302跳转动作,只会把302响应当成普通接口返回交给JS处理,自然不会触发页面刷新。

另外不要尝试在Controller方法内部直接调用showInventories()方法——这只是普通Java方法调用,不会走Spring MVC的路由解析、模型绑定、视图渲染流程,拿到的只是方法返回的字符串user_inventories,完全实现不了页面刷新的效果。

修复方案

根据你的前端提交方式选对应方案即可:

方案1:传统同步表单提交场景(最小改动)

先修正变量笔误,把所有limitCreationResponse替换为inventoryCreationResponse,再把重定向路径改为根路径开头的绝对路径:

// 注意路径前的/不能丢
return ResponseEntity.status(HttpStatus.FOUND).location(URI.create("/inventories")).build();

方案2:异步AJAX提交场景

后端不要返回302响应,统一返回JSON格式的处理结果,由前端拿到成功状态后主动触发页面跳转:

  • 后端修改成功分支返回逻辑:
// 所有分支统一返回JSON结果
if (!inventoryCreated) {
    if (!inventoryItemExists) {
        return ResponseEntity.unprocessableEntity().body("Inventory item not created!!");
    } else {
        return ResponseEntity.ok(inventoryCreationResponse);
    }
}
// 创建成功也返回成功标识
return ResponseEntity.ok(inventoryCreationResponse);
  • 前端在请求成功回调里加跳转逻辑:
// 以fetch请求为例
fetch('/create_inventory_record', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(formParams)
})
.then(res => res.json())
.then(res => {
    if (res.ItemCreated) {
        // 创建成功后主动跳转页面
        window.location.href = '/inventories'
    }
    // 其余业务提示逻辑,比如记录已存在的弹窗提醒
})
更优实现(Spring MVC 原生PRG模式)

如果你用Thymeleaf/JSP等服务端模板渲染,推荐直接用Spring MVC自带的重定向视图能力,不需要手动拼ResponseEntity,这也是官方推荐的POST提交后跳转实现,天然规避路径拼接错误,还能避免用户刷新页面重复提交表单:

@PostMapping("/create_inventory_record")
public String createInventoryRecord(@RequestBody MultiValueMap<String, String> inventoryRecord,
                                    RedirectAttributes redirectAttributes) throws Exception {
    // 省略参数封装、业务调用逻辑
    HashMap<String, String> inventoryCreationResponse = inventoryService.createInventoryRecord(newInventory);
    boolean inventoryCreated = Boolean.parseBoolean(inventoryCreationResponse.get("ItemCreated"));
    boolean inventoryItemExists = Boolean.parseBoolean(inventoryCreationResponse.get("ItemAlreadyExists"));

    if (!inventoryCreated) {
        if (!inventoryItemExists) {
            // 用Flash属性传递页面提示信息,重定向后不会丢失
            redirectAttributes.addFlashAttribute("error", "Inventory item not created!!");
        } else {
            redirectAttributes.addFlashAttribute("tip", "Inventory item already exists");
        }
    } else {
        redirectAttributes.addFlashAttribute("success", "Inventory item created successfully");
    }
    // 直接返回重定向视图,框架自动处理302响应和路径解析
    return "redirect:/inventories";
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 03:51:23