如果 URL Handkler 方法的返回类型不是 String,如何从 Spring Controller 发送重定向。
@RequestMapping(value = "/save", method = RequestMethod.POST)
@ResponseBody
public JsonResponse save(@RequestBody FormBean formBean) {
if(condition true)
? Save(formBean)
: "redirect:/anotherUrl";
}
所以作为返回类型的 JsonResponse 实际上是一个 java 类,我如何在此处发送重定向?
最佳答案
您必须返回 ResponseEntity
从你的 Controller 方法。它有一个 HttpStatus‘ and headers you can set, and a body that is your domain object. To redirect you would normally set the status to
找到(302) and add a
Location` header ,其中包含客户端要重定向的 URL。
关于java - 从 Spring Controller 发送重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22721334/