java - 从 Spring Controller 发送重定向

标签 java spring spring-mvc

如果 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/

相关文章:

java - 如何使用 Spring Controller 中设置的值填充隐藏表单字段

java - Jackson JSON 未设置 Map<String, Object> 值

java - 随机长整型始终是最小值

java - 如何覆盖 RequestParam 的默认行为?

java - 列表中每个对象的 Thymeleaf 形式结果为 "Neither BindingResult nor plain target object"

java - 使用 Spring 和 Hibernate 的 MVC webapp 中的 org.springframework.beans.factory.BeanCreationException

java - Spring-security/登录重定向

java - 泰坦警告 : Query requires iterating over all vertices [(name <> null)]

java - Java 中的 "Constructor call must be the first statement in a constructor"问题

Spring MVC GetMapping 不生效