【问题标题】:Display SpringBoot Actuator JSON string as HTML将 Spring Boot Actuator JSON 字符串显示为 HTML
【发布时间】:2020-09-24 12:03:45
【问题描述】:

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

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

如果有一些错误,我也会显示出来。 谢谢

【问题讨论】:

  • 没有什么是开箱即用的,你需要有一个你自己的端点,你可以调用它,然后调用执行器端点并将json转换为html

标签: spring-boot spring-boot-actuator


【解决方案1】:

您可以覆盖 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 How to convert a json object into HTML format in java?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2020-02-07
    • 2014-09-04
    • 2016-07-07
    • 2020-05-27
    • 1970-01-01
    相关资源
    最近更新 更多