4

我有一个应用程序配置为在 /manage/health 处为 Spring Boot Actuator 的健康端点提供服务。不幸的是,由于我要部署的基础设施的一些细节,我需要将 / 和 /health 都别名为 /manage/health。

我没有看到通过属性仅自定义健康端点 URL 的选项。我假设无法添加适用于我不拥有的控制器的额外 @RequestMapping 注释。

我更愿意明确定义所需的别名,而不是一些影响所有流量性能的流量拦截器。对 Spring 来说相对较新,我不确定最好的方法是什么,我的搜索并没有把我引向正确的方向。

任何人都可以提供一些方向吗?

谢谢。

4

1 回答 1

13

将 bean 添加到配置中以添加视图控制器。这必须扩展WebMvcConfigurerAdapter并简单地覆盖该addViewControllers方法。

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/manage/health");
        registry.addViewController("/health").setViewName("forward:/manage/health");
    }
}

或者,如果您想强制重定向使用addRedirectViewController而不是addViewController.

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry. addRedirectViewController("/", "/manage/health");
        registry.addRedirectViewController("/health","/manage/health");
    }
}
于 2015-09-10T13:25:07.843 回答