Hitesh Kumar
Hitesh Kumar

Reputation: 514

redirect to POST method in same controller

I have a spring RestController and want to redirect to POST method in same controller with RequestBody. any solution welcomes, not only redirect.

MyController:

@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
public String addCompany(@Valid Company company, BindingResult result,
        HttpServletRequest request, Model model) throws Exception {
    //some logic
    //need to pass Company Object as RequestBody
    return "redirect:/app/postmethod/";
}

//Method to redirected
@RequestMapping(value = "/postmethod", method = {RequestMethod.POST, RequestMethod.GET})
public String getData( @RequestBody(required=false) Company compnay, HttpServletRequest request, Model model) throws Exception {
    //some logic
    //required company object
    return "htmlpage";
}

I need to redirect my request to /postmethod from addCompany method in the same controller, I am open to use any feasible solution.

Upvotes: 0

Views: 5222

Answers (2)

DDovzhenko
DDovzhenko

Reputation: 1315

Check here: https://www.baeldung.com/spring-redirect-and-forward#redirecting-an-http-post-request

As per HTTP 1.1 protocol reference, status codes 301 (Moved Permanently) and 302 (Found) allow the request method to be changed from POST to GET. The specification also defines the corresponding 307 (Temporary Redirect) and 308 (Permanent Redirect) status codes that don't allow the request method to be changed from POST to GET.

@PostMapping("/redirectPostToPost")
public ModelAndView redirectPostToPost(HttpServletRequest request) {
    request.setAttribute(
      View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    return new ModelAndView("redirect:/redirectedPostToPost");
}

@PostMapping("/redirectedPostToPost")
public ModelAndView redirectedPostToPost() {
    return new ModelAndView("redirection");
}

The request body will be passed. Here is an example using your code:

@RestController
@RequestMapping("app")
public class TestController {

    @PostMapping("/addCompany")
    public ModelAndView addCompany(@RequestBody Company company, HttpServletRequest request) {
        System.out.println("First method: " + company.name);
        request.setAttribute(
                View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
        return new ModelAndView("redirect:/app/postmethod/");
    }

    @PostMapping("/postmethod")
    public void getData(@RequestBody Company company) {
        System.out.println("Redirected: " + company.name);
    }

    public static class Company {
        String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

When using POST request to http://localhost:8080/app/addCompany with body {"name": "Test Company"}, in the output I receive next:

First method: Test Company
Redirected: Test Company

Upvotes: 1

石荒人
石荒人

Reputation: 943

I found a good explanation on this page here.

For protecting users against inadvertently (re)submitting a POST transaction which they had not intended, or submitting a POST into a context which they would not have wanted.

So you can set the data in session and using get method to call post method if you want to do this.

Upvotes: 0

Related Questions