Spring Boot2.x如何自定义Endpoint

网友投稿 338 2023-06-23

Spring Boot2.x如何自定义Endpoint

Spring Boot2.x如何自定义Endpoint

概述

在使用Spring Boot的时候我们经常使用actuator,健康检查,bus中使用/refresh等。这里记录如何使用注解的方式自定义Endpoint。可用于满足一些服务状态监控,或者优雅停机等。

准备

Spring Boot项目,pom中加入:

org.springframework.boot

spring-boot-starter-actuator

编写自定义Endpoint

@Configuration

@Endpoint(id = "my-endpoint")

public class MyEndpoint {

@ReadOperation

public Map endpoint() {

Map map = new HashMap<&gtBtASXNPNO;(16);

map.put("message", "this is my endpoint");

return map;

}

}

1.配置

management.endpoints.web.exposure.include=my-endpoint

2.启动&测试

启动后可以看到日志:

Mapped "{[/actuator/my-endpoint],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map)

3.注意

@EndPoint中的id不能使用驼峰法,需要以-分割。

@Spring Boot会去扫描@EndPoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分别对应生成Get/Post/Delete的Mapping。注解中有个produces参数,可以指定media type, 如:application/json等。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Cache扩展功能实现过程解析
下一篇:SpringBoot整合Mybatis的知识点汇总
相关文章

 发表评论

暂时没有评论,来抢沙发吧~