官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 阿尔山市| 潼关县| 大安市| 理塘县| 安仁县| 石渠县| 南安市| 通化县| 阿图什市| 赣州市| 建水县| 金山区| 墨江| 桂林市| 岢岚县| 班玛县| 南丰县| 囊谦县| 乳山市| 大石桥市| 陇西县| 临汾市| 苏尼特左旗| 仁布县| 本溪| 淳安县| 平和县| 永仁县| 祁连县| 江都市| 南川市| 陕西省| 阳泉市| 大庆市| 新乡市| 安乡县| 曲靖市| 长乐市| 二连浩特市| 靖州| 昌黎县|