Redirect Spring MVC获取/重定向/发布

Redirect Spring MVC获取/重定向/发布,redirect,spring-mvc,Redirect,Spring Mvc,假设我有两个Spring MVC服务: @RequestMapping(value = "/firstMethod/{param}", method = RequestMethod.GET) public String firstMethod(@PathVariable String param) { // ... // somehow add a POST param return "redirect:/secondMethod"; } @RequestMapping

假设我有两个Spring MVC服务:

@RequestMapping(value = "/firstMethod/{param}", method = RequestMethod.GET)
public String firstMethod(@PathVariable String param) {
    // ...
    // somehow add a POST param
    return "redirect:/secondMethod";
}

@RequestMapping(value = "/secondMethod", method = RequestMethod.POST)
public String secondMethod(@RequestParam String param) {
    // ...
    return "mypage";
}
能否将第一个方法调用重定向到第二个(POST)方法? 使用第二种方法作为GET或使用session是不可取的


谢谢你的回复

不应将HTTP GET重定向到HTTP POST。HTTPGET和HTTPPOST是两个不同的东西。它们的行为应该非常不同(GET是安全的、幂等的、可缓存的,POST是幂等的)。有关更多信息,请参见示例或

您可以做的是:也使用RequestMethod.GET注释secondMethod。然后您应该能够进行所需的重定向

@RequestMapping(value = "/secondMethod", method = {RequestMethod.GET, RequestMethod.POST})
public String secondMethod(@RequestParam String param) {
...
}

但请注意,然后可以通过HTTP GET请求调用secondMethod

你真的需要重定向吗?为什么不直接从
firstMethod
调用
this.secondMethod(param)
?在本例中,我可以。但是如果
secondMethod
有许多不同的参数,比如
@CookieValue
,那就不方便了。@是的,我不能直接调用java方法的主要原因是我想让用户在浏览器地址栏中有
/secondMethod
。然后重定向就是一种方法。。。这意味着
MethodRequest.GET
,因为调用POST方法n次将导致n次更新,所以POST不是幂等的。