spring boot actuator endpoint mapping root class

Harshil Source

In spring we can design rest web service like below.

@RestController
public class HelloController {
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Hello");
        return "hello";
    }
}

When we do so, @RestController & @RequestMapping will internally manage request mapping part. So when I will hit url i.e. http://localhost:8080/hello it will point to printWelcome method.

I was looking into spring boot actuator source code. If we will use spring boot actuator in our application it will provide us some endpoints, which has exposed as rest APIs like health, metrics, info. So in my application if I am using spring boot actuator, when I will hit the url like "localhost:8080/health" I will get response.

So now my question is in spring boot actuator source code where this URLs get mapped. I have debugged source code of spring boot actuator, but not able to find out the root class of mapping of endpoints.

Can anyone please help ?

javaspringspring-bootspring-boot-actuator

Answers

answered 2 years ago Purna #1

here it is , In AbstractEndpoint it says

/**
     * Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped
     * to a URL (e.g. 'foo' is mapped to '/foo').
     */

If you see HealthEndPoint it extends AbstractEndpoint and does a super("health", false); , thats where it maps to "localhost:8080/health".

answered 2 years ago Pau #2

All spring-boot-actuator endpoints extends AbstractEndpoint (In Health endpoint case for example: class HealthEndpoint extends AbstractEndpoint<Health>) which construcor has the id of the Endpoint.

 /**
 * Endpoint identifier. With HTTP monitoring the identifier of the endpoint is mapped
 * to a URL (e.g. 'foo' is mapped to '/foo').
 */
private String id;

Otherwise, it has an invoke method (from interface Endpoint) through it is invoked the endpoint.

/**
 * Called to invoke the endpoint.
 * @return the results of the invocation
 */
T invoke();

Finally, this endpoints are configured in the class EndpointAutoConfiguration as Bean:

@Bean
@ConditionalOnMissingBean
public HealthEndpoint healthEndpoint() {
    return new HealthEndpoint(this.healthAggregator, this.healthIndicators);
}

Take a look this post where explains how to custom your endpoint:

comments powered by Disqus