SpringBoot项目@RestController使用 redirect 重定向无效

xiaoxiao2025-04-04  24

Spring MVC项目中页面重定向一般使用return "redirect:/other/controller/";即可。

而Spring Boot当我们使用了@RestController注解,上述写法只能返回字符串,解决方法如下:

将一个HttpServletResponse参数添加到处理程序方法然后调用 response.sendRedirect("some-url");

@RestController public class LoginController { @RequestMapping("/logout") void handleLogout(HttpServletResponse response) throws IOException { response.sendRedirect("some-url"); } }

 

参考:

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

转载请注明原文地址: https://www.6miu.com/read-5027438.html

最新回复(0)