Spring Boot2.x如何自定義Endpoint
概述
在使用Spring Boot的時(shí)候我們經(jīng)常使用actuator,健康檢查,bus中使用/refresh等。這里記錄如何使用注解的方式自定義Endpoint。可用于滿足一些服務(wù)狀態(tài)監(jiān)控,或者優(yōu)雅停機(jī)等。
準(zhǔn)備
Spring Boot項(xiàng)目,pom中加入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
編寫自定義Endpoint
@Configuration@Endpoint(id = 'my-endpoint')public class MyEndpoint { @ReadOperation public Map<String, Object> endpoint() { Map<String, Object> map = new HashMap<>(16); map.put('message', 'this is my endpoint'); return map; }}
1.配置
management.endpoints.web.exposure.include=my-endpoint
2.啟動(dòng)&測(cè)試
啟動(dòng)后可以看到日志:
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<java.lang.String, java.lang.String>)
3.注意
@EndPoint中的id不能使用駝峰法,需要以-分割。 @Spring Boot會(huì)去掃描@EndPoint注解下的@ReadOperation, @WriteOperation, @DeleteOperation注解,分別對(duì)應(yīng)生成Get/Post/Delete的Mapping。注解中有個(gè)produces參數(shù),可以指定media type, 如:application/json等。以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 理解PHP5中static和const關(guān)鍵字2. Android table布局開發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器3. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼4. IntelliJ IDEA安裝插件的方法步驟5. php模擬實(shí)現(xiàn)斗地主發(fā)牌6. vue 使用localstorage實(shí)現(xiàn)面包屑的操作7. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)8. Python random庫使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. Vuex localStorage的具體使用

網(wǎng)公網(wǎng)安備