我有一个应用程序配置为在/manage/health 处为 Spring Boot Actuator 的健康端点提供服务。不幸的是,由于我要部署的基础设施的一些细节,我需要将/和/health 都别名为/manage/health。
我没有看到通过属性仅自定义健康端点 URL 的选项。我假设无法添加适用于我不拥有的 Controller 的额外 @RequestMapping 注释。
我更愿意明确定义所需的别名,而不是一些影响所有流量性能的流量拦截器。对 Spring 来说相对较新,我不确定最好的方法是什么,我的搜索并没有把我引向正确的方向。
任何人都可以提供一些方向吗?
谢谢。
最佳答案
将 bean 添加到配置中以添加 View Controller 。这必须扩展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");
}
}
关于spring-mvc - 将多个 URL 路由到 Spring Boot Actuator 健康端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32502908/