5
votes

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!

3

3 Answers

12
votes

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?

0
votes

The initial partly problem was that the code did not add an endpoint with no "selector"

Source

@Endpoint(id = "loggers")
@Component
public class LoggersEndpoint {

    @ReadOperation
    public Map<String, Object> loggers() { ... }

    @ReadOperation
    public LoggerLevels loggerLevels(@Selector String name) { ... }

    @WriteOperation
    public void configureLogLevel(@Selector String name, LogLevel configuredLevel) { ... }

}

This endpoint exposes three operations:

GET on /application/loggers: the configuration of all loggers (as it has no "selector" parameter):

GET on /application/loggers/{name}: the configuration of a named logger (using the name @Selector).

...

The edited question led to the conclusion that it should be a bean

If you add a @Bean annotated with @Endpoint, any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and, in a web application, over HTTP as well. Endpoints can be exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux.

Or see comments in the announcement of the feature

0
votes

Maybe this will help somebody.

Looks like @ReadOperation doesn't support return type Void. You should return at least an empty string on your invoke method.

spring-boot 2.0.3.RELEASE

@Component
@Endpoint(id = "heartbeat")
public class HeartbeatEndpoint {

    @ReadOperation
    public String invoke() {
        return "";
    }
}