Nailgun
Nailgun

Reputation: 4169

Spring MVC GET/redirect/POST

Say I have 2 Spring MVC services:

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

Could redirect the first method call to second(POST) method? Using second method as GET or using session is undesirable.

Thanks for your responses!

Upvotes: 7

Views: 13236

Answers (1)

nanoquack
nanoquack

Reputation: 969

You should not redirect a HTTP GET to a HTTP POST. HTTP GET and HTTP POST are two different things. They are expected to behave very differently (GET is safe, idempotent and cacheable. POST is idempotent). For more see for example HTTP GET and POST semantics and limitations or http://www.w3schools.com/tags/ref_httpmethods.asp.

What you can do is this: annotate secondMethod also with RequestMethod.GET. Then you should be able to make the desired redirect.

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

But be aware that secondMethod can then be called through HTTP GET requests.

Upvotes: 2

Related Questions