spring-boot - 将 SpringBoot Actuator JSON 字符串显示为 HTML
问题描述
如何将 SpringBoot Actuator 中的 JSON 字符串显示为 HTML?所以当我访问 url localhost:8080/actuator/health 我看到类似
Application status: UP
Database H2: UP
Database MySQL: UP
Diskspace: UP
如果有一些错误,我也会显示它们。谢谢
解决方案
您可以覆盖 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 格式?
推荐阅读
- python - 始终返回 iterator() 的 Django QuerySet
- asp.net-mvc - ajax表单提交后引导导航栏菜单不起作用
- c# - DBF FoxPro 缺少子句组
- mysql - MySql xdevapi 与 node.js:“服务器连接未使用 X 协议”
- php - 如何在 PHP 中回显 INNER 加入
- emacs - 键绑定改变角色的脸
- regex - 一个正则表达式来检测/删除两个最外部的“(双引号)
- sql - 我将 json 文件插入到我的表中,但是我如何将日期也放入我的表中?
- bash - 如何改进这个 bash 脚本?
- javascript - 带有来自 csv 的值的直方图