【问题标题】:Spring Boot "redirect:/path" doesn't work - actually returning the String "redirect:/path"Spring Boot "redirect:/path" 不起作用 - 实际上返回字符串 "redirect:/path"
【发布时间】:2021-11-18 22:16:15
【问题描述】:

我正在尝试从我的一个控制器重定向到另一个控制器。它应该与关键字“redirect”一起使用,但不是重定向到其他控制器,而是得到以下响应:

这是我的控制器:

@RestController
@Api(value = ConsentConstants.GET_HEALTH_V1, tags = {ConsentConstants.SWAGGER_API})
@Slf4j
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GetHealthController implements GetHealthApi {

  final Tracer tracer;

  @Override
  public Object getHealthCheck() {
    return "redirect:/actuator/health";
  }
}

我尝试重定向的原因是,当我部署我的服务时,运行状况检查(弹簧启动执行器)不起作用。在本地它工作正常,但在部署后我收到 /actuator/health 端点的 401 未授权(其他端点工作正常)。所以我想尝试使用其中一个工作端点(例如状态/健康,然后重定向到 /actuator/health)

我没有使用 Spring MVC,这有什么不同吗?

【问题讨论】:

    标签: spring-boot spring-boot-actuator


    【解决方案1】:

    这种重定向方式仅适用于常规控制器,不适用于休息服务。无法解析名为 /actuator/health 的“视图”。

    这仅适用于存在要解析的注册文件名的视图:

    return "redirect:registration";
    

    要在 restcontroller 中重定向,您可以执行以下操作:

    @GetMapping("/healthcheck")
    public void getHealthCheck(HttpServletResponse response) throws IOException {
      response.sendRedirect("/actuator/health");
    }
    

    但是,如果您只是想更改 Spring 执行器的健康端点,请参阅 here

    在 application.properties 中:

    management.endpoints.web.base-path=/whatever
    management.endpoints.web.path-mapping.health=healthcheck
    

    【讨论】:

    • 但是,我认为这对您的情况没有帮助,因为您只会被重定向到之前的故障点。我认为还有其他事情阻止您到达终点。不过,我真的无法从您的问题中描述的内容中分辨出什么。
    • response.sendRedirect("/actuator/health") 工作,谢谢。我之前尝试过management.endpoints.web.base-path=/whatever management.endpoints.web.path-mapping.health=healthcheck,出现了一些问题,因为我们的部署环境也使用了healthcheck来检查服务是否仍然正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2020-06-25
    相关资源
    最近更新 更多