27 Temmuz 2020 Pazartesi

SpringBoot Actuator Custom Endpoint @ReadOperation Anotasyonu

Giriş
Şu satırı dahil ederiz
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
Açıklaması şöyle
Any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and HTTP. Even you can expose technology-specific endpoint by using @JmxEndpoint or @WebEndpoint.
Örnek
Şöyle yaparız
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@org.springframework.boot.actuate.endpoint.annotation.Endpoint(id = "say-hello")
public class Endpoint {

  @ReadOperation
  public String sayHello() {
    return "Hello World";
  }
}
Örnek - Map Dönen Custom Endpoint
Şöyle yaparız
@Endpoint(id = "custom")
@Component
public class CustomEndpoint {

  @ReadOperation
  public Map<String, String> customEndpoint() {
    Map<String, String> map = new HashMap<>();
    map.put("k1", "v1");
    map.put("k2", "v2");
    return map;
  }
}
Örnek - Parametre Alan
Şöyle yaparız
@ReadOperation
public String helloName(String name) {
  return "Hello " + name;
}
Çağırmak için şöyle yaparız
> curl 'http://localhost:8080/actuator/helloworld?name=Jamie'
Hello Jamie
Örnek - Path Variable
Açıklaması şöyle
A @ReadOperation method that takes a single argument which is annotated with @Selector.
@Selector annotation maps to path variables.
Şöyle yaparız
@ReadOperation
public String helloNameSelector(@Selector String name) {
  return "Hello " + name;
}
Çağırmak için şöyle yaparız
> curl 'http://localhost:8080/actuator/helloworld/Alex'
Hello Alex

Hiç yorum yok:

Yorum Gönder