我正在编写一个仅包含 Rest API 端点的 Web 应用程序。该应用程序没有提供jsp,也没有任何用Java编写的UI。 (以后打算用React写前端)
在我的 Controller 中使用 ModelAndView
有意义吗?我想重定向到另一个 URL?我看到类似于以下代码的示例代码:
@Controller
@RequestMapping("/")
public class RedirectController {
@GetMapping("/redirectWithRedirectPrefix")
public ModelAndView redirectWithUsingRedirectPrefix(ModelMap model) {
model.addAttribute("attribute", "redirectWithRedirectPrefix");
return new ModelAndView("redirect:/redirectedUrl", model);
}
}
最佳答案
如果你的 Controller 总是要重定向,你可以只返回一个字符串,例如
return "redirect:/redirectedUrl";
否则,您可以返回响应实体:
final HttpHeaders headers = new HttpHeaders();
headers.add("Location", "/redirectedUrl");
return new ResponseEntity<Foo>(headers, HttpStatus.FOUND);
我认为如果没有 View ,返回 ModelAndView 是没有意义的。
关于java - 在 Rest API 应用程序中使用 ModelAndView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928975/