Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot 2 Custom Actuator Endpoints

Spring Noob: OK. I start with a STS Spring Starter Project / Maven / Java 8 / Spring Boot 2.0, and select the Web and Actuator dependencies. It builds and runs fine, and reponds to http://localhost:8080/actuator/health. I add an "Endpoint" to the main application class, so that it looks like this.

package com.thumbsup;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class YourStash11Application {

    public static void main(String[] args) {
        SpringApplication.run(YourStash11Application.class, args);
    }

    @Endpoint(id="mypoint")
    public class CustomPoint {
        @ReadOperation
        public String getHello(){
            return "Hello" ;
        }
    }

}

I try to enable everything in application.properties:

management.endpoints.enabled-by-default=true
management.endpoint.conditions.enabled=true
management.endpoint.mypoint.enabled=true
management.endpoints.web.exposure.include=*

But when it builds, there's no reference to mapping /actuator/mypoint, and
http://localhost:8080/actuator/mypoint and
http://localhost:8080/application/mypoint
both return 404 errors.

What am I missing? Thanks!

like image 942
Gene Knight Avatar asked Mar 20 '18 15:03

Gene Knight


People also ask

How can we create a custom endpoint in spring boot actuator?

Spring Actuator Custom Endpoints We can create our own custom actuator endpoints using @Endpoint annotation on a class. Then we have to use @ReadOperation , @WriteOperation , or @DeleteOperation annotations on the methods to expose them as actuator endpoint bean.

How can we expose custom endpoints in spring boot select all possibilities?

In a Spring Boot application, we expose a REST API endpoint by using the @RequestMapping annotation in the controller class. For getting these endpoints, there are three options: an event listener, Spring Boot Actuator, or the Swagger library.

Which are the most common spring boot actuator endpoints?

Here are some of the most common Spring Boot Actuator endpoints: /health: It shows application health information. /info: It displays arbitrary application info. /metrics: It gives all metrics related information for the current application.


1 Answers

OK, solved:

    @Endpoint(id="mypoint")
    @Component
    public class myPointEndPoint {
        @ReadOperation
        public String mypoint(){
            return "Hello" ;
        }
    }

What was missing was the "@Component" annotation. But, where is this in the docs?

like image 159
Gene Knight Avatar answered Sep 30 '22 07:09

Gene Knight