Spring Boot2.x如何自定义Endpoint
概述
在使用SpringBoot的时候我们经常使用actuator,健康检查,bus中使用/refresh等。这里记录如何使用注解的方式自定义Endpoint。可用于满足一些服务状态监控,或者优雅停机等。
准备
SpringBoot项目,pom中加入:
org.springframework.boot spring-boot-starter-actuator
编写自定义Endpoint
@Configuration @Endpoint(id="my-endpoint") publicclassMyEndpoint{ @ReadOperation publicMapendpoint(){ Map map=newHashMap<>(16); map.put("message","thisismyendpoint"); returnmap; } }
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]}"ontopublicjava.lang.Objectorg.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map
)
3.注意
- @EndPoint中的id不能使用驼峰法,需要以-分割。
- @SpringBoot会去扫描@EndPoint注解下的@ReadOperation,@WriteOperation,@DeleteOperation注解,分别对应生成Get/Post/Delete的Mapping。注解中有个produces参数,可以指定mediatype,如:application/json等。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。