Spring Boot項目@RestController使用重定向redirect


Spring MVC項目中頁面重定向一般使用return "redirect:/other/controller/";即可。而Spring Boot使用了@RestController注解,上述寫法只能返回字符串,解決方法如下:

將一個HttpServletResponse參數添加到處理程序方法然后調用response.sendRedirect("some-url");

@RestController
public class FooController {

  @RequestMapping("/foo")
  void handleFoo(HttpServletResponse response) throws IOException {
    response.sendRedirect("some-url");
  }

}

 

參考:

https://stackoverflow.com/questions/29085295/spring-mvc-restcontroller-and-redirect


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM