You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用Spring Boot 2 Actuator的发现页面?

解决Spring Boot 2.0 Actuator发现页面无法覆盖的问题

嘿,我之前也碰到过这个头疼的问题,刚好知道几个能解决的办法,给你参考下:

方法一:直接通过配置禁用发现页面

这是最简单直接的方式,Spring Boot 2.x提供了专门的配置项来关闭Actuator的自动发现页面。你只需要在配置文件里添加一行配置就行:

如果用的是application.properties:

management.endpoints.web.discovery.enabled=false

如果是application.yml:

management:
  endpoints:
    web:
      discovery:
        enabled: false

配置生效后,Actuator就不会再在基础路径生成那个默认的发现页面了,你自定义的页面映射就能正常生效。

方法二:通过自定义配置提高自定义页面的优先级(备用方案)

如果上面的配置因为某些原因没生效,你可以通过自定义WebMvc配置,让你的页面映射优先级超过Actuator的:

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CustomActuatorPageConfig implements WebMvcConfigurer, Ordered {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 这里替换成你的Actuator基础路径,比如"/actuator"
        registry.addViewController("/actuator")
                // 替换成你的自定义页面路径,比如"custom-overview"(假设你有对应的视图)
                .setViewName("custom-overview");
    }

    @Override
    public int getOrder() {
        // 设置最高优先级,确保比Actuator的映射先被处理
        return Ordered.HIGHEST_PRECEDENCE;
    }
}

这个方法相当于强制把请求转发到你的自定义页面,绕过Actuator的默认发现页面。

一般来说第一种方法就足够解决问题了,毕竟是官方提供的配置项,更稳妥。

内容的提问来源于stack exchange,提问作者Riccardo Lippolis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:34:37