首页 > 解决方案 > 将 SpringBoot Actuator JSON 字符串显示为 HTML

问题描述

如何将 SpringBoot Actuator 中的 JSON 字符串显示为 HTML?所以当我访问 url localhost:8080/actuator/health 我看到类似

Application status: UP
Database H2:        UP
Database MySQL:     UP
Diskspace:          UP

如果有一些错误,我也会显示它们。谢谢

标签: spring-bootspring-boot-actuator

解决方案


您可以覆盖 EndpointWebExtension 的现有定义,如下所示。您可以根据需要编写自定义逻辑。

@Component

@EndpointWebExtension(endpoint = HealthEndpoint.class)

public class CustomeEndpointWebExtension extends HealthEndpointWebExtension {

    private static final String[] NO_PATH = {};
    public HealthWebEndpointWebExtension(HealthContributorRegistry registry, HealthEndpointGroups groups) {
        super(registry, groups);

    }

//To write custom logic for /acuator/health
    @ReadOperation
    public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext) {
        System.out.println("#@Start");
        WebEndpointResponse<HealthComponent> health = health(apiVersion, securityContext, false, NO_PATH);
        //Can write customer logic here as per requirment
        System.out.println("#@End"+health.getBody().getStatus());
        return health;
    }

//To write custom logic for /acuator/health/db
    @ReadOperation
    public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext,
            @Selector(match = Match.ALL_REMAINING) String... path) {
        WebEndpointResponse<HealthComponent> health = health(apiVersion, securityContext, false, path);
        System.out.println("#Status"+health.getBody().getStatus());
        return health;
    }

}

您可以参考下面的堆栈将 json 转换为 html 如何在 java 中将 json 对象转换为 HTML 格式?


推荐阅读