McQueen
McQueen

Reputation: 490

Sending redirect from Spring controller

How to send the redirect from Spring controller if the return type is not String for the URL Handkler method.

@RequestMapping(value = "/save", method = RequestMethod.POST)
@ResponseBody
public JsonResponse save(@RequestBody FormBean formBean) {  

    if(condition true)
      ? Save(formBean)
      : "redirect:/anotherUrl";

}

So JsonResponse which is a return type is actually a java class, how do i send redirect here ?

Upvotes: 1

Views: 1057

Answers (1)

Dave Syer
Dave Syer

Reputation: 58094

You have to return a ResponseEntity from your controller method. It has an HttpStatus‘ and headers you can set, and a body that is your domain object. To redirect you would normally set the status toFOUND(302) and add aLocation` header with the URL for the client to redirect.

Upvotes: 1

Related Questions