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