簡體   English   中英

在Rest API應用程序中使用ModelAndView

[英]Use ModelAndView in a Rest API app

我正在編寫一個僅由Rest API端點組成的Web應用程序。 該應用程序沒有提供jsp,也沒有任何用Java編寫的UI。 (我計划將來在React中編寫前端)

在我的控制器中使用ModelAndView是否有意義? 我想重定向到另一個URL嗎? 我看到類似於以下代碼的示例代碼:

@Controller
@RequestMapping("/")
public class RedirectController {

    @GetMapping("/redirectWithRedirectPrefix")
    public ModelAndView redirectWithUsingRedirectPrefix(ModelMap model) {
        model.addAttribute("attribute", "redirectWithRedirectPrefix");
        return new ModelAndView("redirect:/redirectedUrl", model);
    }
}

如果您的控制器總是要重定向,則可以返回一個String,例如

return "redirect:/redirectedUrl";

否則,您可以返回一個響應實體:

final HttpHeaders headers = new HttpHeaders();
headers.add("Location", "/redirectedUrl");    
return new ResponseEntity<Foo>(headers, HttpStatus.FOUND);

我認為如果沒有視圖,則返回ModelAndView是沒有意義的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM