Routing multiple URLs to Spring Boot Actuator's health endpoint

删除回忆录丶 提交于 2020-01-23 13:04:18

问题


I have an app configured to serve Spring Boot Actuator's health endpoint at /manage/health. Unfortunately due to some details of the infrastructure I'm deploying to I need to alias both / and /health over to /manage/health.

I don't see an option to customize just the health endpoint URL via properties. I'm assuming there's no way to add extra @RequestMapping annotations that apply to a controller I don't own.

I'd prefer to explicitly define the required aliases as opposed to some traffic interceptor that affects the performance of all traffic. Being relatively new to Spring, I'm unsure what the best way is to proceed and my searches aren't leading me in the right direction.

Can anyone provide some direction?

Thanks.


回答1:


Add a bean to the configuration to add a view controller. This been must extend WebMvcConfigurerAdapter and simply override the addViewControllers method.

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

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

Or if you want to force a redirect use addRedirectViewController instead of addViewController.

@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry. addRedirectViewController("/", "/manage/health");
        registry.addRedirectViewController("/health","/manage/health");
    }
}


来源:https://stackoverflow.com/questions/32502908/routing-multiple-urls-to-spring-boot-actuators-health-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!