Spring RedirectAttributes无法与@PostMapping(value = {“ /”,“ / {id}”})一起使用

时间:2020-10-09 11:35:48

标签: angular spring-boot spring-mvc java-11

我有一个像这样的控制器:

@PostMapping(value = { "/" })
public String home(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
return "redirect:/index.html?xyz=abc";
}

它正在将请求重定向到Angular应用程序,并且按预期方式工作。 但是,一旦我将PostMapping批注更改为:

@PostMapping(value = { "/", "/{id}" })

在Chrome控制台中,它开始给我405错误:

无法加载资源:服务器响应状态为405() https://My_IP/index.html?xyz = abc

有人可以建议我在这里想念什么吗? 是否需要在Java端或Angular端进行修复?

角度版本:8.1.3

RedirectAttributes来自org.springframework.web.servlet.mvc.support.RedirectAttributes

1 个答案:

答案 0 :(得分:0)

在@postmapping中添加“ / {id}”后,系统会尝试将get redirect请求与/ id本身进行匹配,因为它假定“ index.html”为id。 而且,由于它是一个获取重定向,而/ id是发布请求,因此它给出了405错误。

我通过不直接使用'/'来避免此问题,而是为其添加了API名称,例如:

main

此后,系统未尝试将https://My_IP/index.html?xyz = abc请求与/ home或/ home / id匹配。