Search code examples
spring-mvchttp-status-code-405

Spring 3 -- how to do a GET request from a forward


I'm trying to forward a request to another Spring controller that takes a GET request, but it's telling me POST is not supported. Here is the relevant portion from my first controller method, which does take a POST request, since I'm using it for a login function.

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("administrator") Administrator administrator,
    Model model) {
    // code that's not germane to this problem
    return "forward:waitingBulletins";
}

Here is the method I'm trying to forward to.

@RequestMapping(value = "/waitingBulletins", method = RequestMethod.GET)
public String getWaitingBulletins(Model model) {
        // the actual code follows
    }

Here is the error message in my browser.

HTTP Status 405 - Request method 'POST' not supported

--------------------------------------------------------------------------------

type Status report

message Request method 'POST' not supported

description The specified HTTP method is not allowed for the requested resource (Request method 'POST' not supported).

Solution

  • forward maintains the original request intact, so you are forwarding a POST request and missing a handler for it.

    By the looks of it, what you're really trying to implement is the POST-redirect-GET pattern, which uses redirect instead of forward.

    You only need to change your POST handler to:

    @RequestMapping(value = "/login", method = RequestMethod.POST) 
    public String login(@ModelAttribute("administrator") Administrator administrator,
        Model model) {
        // code that's not germane to this problem
        return "redirect:waitingBulletins";
    }
    

    to make it work.