首页 > 解决方案 > 我无法使用 Spring Boot 重定向

问题描述

我使用 Spring boot @scheduled 定期执行任务。我想在任务的定期执行完成后重定向并跳转到另一个页面。但是,该过程会定期执行并显示消息,但不会执行重定向。请给我提意见。

   enter code here  
   @Controller
   @RequestMapping("/index")
   public class TaskController {  
       @Scheduled(fixedRate = 5000)
       public String task(){
              System.out.println("test");
              return "redirect:/index";
       }
   }

标签: javaspringspring-boot

解决方案


一个带有 schedule 注释的方法正在您的应用程序中运行,而无需与外部进行任何交互。所以没有客户端可以重定向到任何东西。
您只能从被调用的方法重定向,因为它映射到 get 或 put 或任何其他类型的请求。


推荐阅读