- Mastering Spring Boot 2.0
- Dinesh Rajput
- 284字
- 2021-06-25 21:29:20
Creating a custom endpoint
We have seen that the Actuator provides several endpoints for your application. But Spring Boot's Actuator also allows you to create a custom endpoint by implementing the EndPoint interface. Let's see the following example:
package com.dineshonjava.sba; import java.util.ArrayList; import java.util.List; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.stereotype.Component; @Component public class MyCustomEndpoint implements Endpoint<List<String>>{ @Override public String getId() { return "myCustomEndpoint"; } @Override public List<String> invoke() { // Custom logic to build the output List<String> list = new ArrayList<>(); list.add("App message 1"); list.add("App message 2"); list.add("App message 3"); list.add("App message 4"); return list; } @Override public boolean isEnabled() { return true; } @Override public boolean isSensitive() { return true; } }
As you can see, the MyCustomEndpoint class implemented the EndPoint interface and it overrode four methods, getId(), invoke(), isSensitive(), and isEnabled(). The getId() method returns endpoint ID or name, and by using it you can access /myCustomEndpoint for now. Let's see what the following response returns:
[ "App message 1", "App message 2", "App message 3", "App message 4" ]
The invoke() method returns an application message—whatever you want to expose from this custom endpoint. The isEnabled() and isSensitive() methods are used for enabling this endpoint for your application and setting the sensitivity of this endpoint respectively.
There are many more ways of customizing Spring Boot's Actuator. Spring Boot allows us to customize all of the Actuator. That is why Spring Boot is opinionated.
Many of the Actuator endpoints expose sensitive data, so you have to protect these endpoints from any unwanted activity. Spring Boot allows you to secure these Actuator endpoints. In the next section, let's see how to make these Actuator endpoints secure.
- Word/Excel/PPT 2007辦公技巧
- MATLAB 2020中文版從入門到精通
- 和秋葉一起學(xué):秒懂WPS文字處理
- MATLAB R2020a完全自學(xué)一本通
- Word+Excel+PPT高效辦公從入門到精通
- Office 2007辦公軟件應(yīng)用立體化教程
- Office辦公高手應(yīng)用技巧
- Learning AWS IoT
- 中文PowerPoint 2003應(yīng)用基礎(chǔ)
- Office商務(wù)辦公實(shí)用教程
- Excel高手捷徑 一招鮮 吃遍天
- INSTANT Prezi Starter
- Access VBA活用范例手冊(cè)
- Excel 2010 SQL完全應(yīng)用
- PowerPoint 2010從入門到精通