Tutorial

Spring Boot Actuator Endpoints

Published on August 3, 2022
Default avatar

By Pankaj

Spring Boot Actuator Endpoints

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Spring Boot Actuator Endpoints lets us monitor and interact with our application. Spring Actuator is a Spring Boot sub-module and provides built-in endpoints that we can enable and disable for our application.

Spring Boot Actuator Endpoints

Spring Boot Actuator Endpoints are exposed over JMX and HTTP, most of the times we use HTTP based Actuator endpoints because they are easy to access over the browser, CURL command, shell scripts etc. Some of the useful actuator endpoints are:

  1. beans: this endpoint returns the list of all the beans configured in our application.
  2. env: provides information about the Spring Environment properties.
  3. health: Shows application health
  4. info: Displays application information, we can configure this in Spring environment properties.
  5. mappings: Displays the list of all @RequestMapping paths.
  6. shutdown: allows us to gracefully shutdown the application.
  7. threaddump: provides the thread dump of the application.

You can get the complete list of spring actuator endpoints from here.

Spring Actuator Endpoints Security

Only “health” and “info” endpoints are exposed without any security, for accessing all other endpoints we need to configure our application for spring security. This is very easy to achieve, we will get to it in the later part of the tutorial.

Enable Spring Actuator Endpoints

When we add Spring Actuator Dependencies to our spring boot project, it automatically enables actuator endpoints. Add below dependencies to your spring application to enable spring boot actuator endpoints.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Now when you will run the application, you will see actuator endpoints being mapped in the logs.

2018-06-19 15:23:20.715  INFO 6493 --- [main] o.s.b.a.e.web.EndpointLinksResolver: Exposing 2 endpoint(s) beneath base path '/actuator'
2018-06-19 15:23:20.723  INFO 6493 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-06-19 15:23:20.724  INFO 6493 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

Notice that only two endpoints - health and info has been mapped. Below image shows the output of these actuator endpoints. spring boot actuator endpoints health spring boot actuator endpoint info Did you noticed that there is no data in the /actuator/info, it’s because we haven’t configured them. Just add following properties to your application.properties file.

info.app.name=Spring Actuator Example
info.app.java.version=10
info.app.type=Spring Boot

Restart the application and you will get following output: spring boot actuator info properties value

Customizing Actuator End Points Base Path

By default base-path of actuator endpoints is /actuator, we can change it to any other value by setting management.endpoints.web.base-path in application properties file.

management.endpoints.web.base-path=/management

Exposing Other Actuator Endpoints

We can enable and disable other actuator endpoints through property files. If you want to enable all actuator endpoints, then add following property.

management.endpoints.web.exposure.include=*

To enable only specific actuator endpoints, provide the list of endpoint id.

management.endpoints.web.exposure.include=health,info,beans,env

Spring Security for Actuator Endpoints

Note that we need to add Spring Security to our application for enabling additional endpoints because all other endpoints need at least basic authentication. Add following dependency for spring security in your application.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

Also, add spring security username and password in application properties file.

spring.security.user.name=pankaj
spring.security.user.password=pankaj

Restart the application and you will see additional endpoints being mapped.

2018-06-19 16:18:22.211  INFO 6627 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/beans],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-06-19 16:18:22.212  INFO 6627 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/env],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-06-19 16:18:22.212  INFO 6627 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/env/{toMatch}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

Now when you will try to access the secured actuator endpoints, you will have to provide login credentials. spring boot actuator endpoint security username password Below images shows the response of beans and env/java.home endpoints. spring boot actuator endpoint beans spring boot actuator endpoint env java.home

Spring Actuator Custom Endpoints

One of the great features of Spring Framework is that it’s very easy to extend. 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. We can create technology-specific Endpoints using @JmxEndpoint and @WebEndpoint annotations. Here is an example of our own custom spring actuator endpoint.

package com.journaldev.spring;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Endpoint(id="myendpoint")
@Component
public class MyCustomEndpoints {

	@ReadOperation
	@Bean
	public String hi() {
		return "Hi from custom endpoint";
	}
}

Did you notice the endpoint id? We also need to configure this in the list of actuator endpoints to be enabled. Update following properties in application.properties file.

management.endpoints.web.exposure.include=health,info,beans,env,myendpoint

Now when you will start the application, check for this new endpoint being mapped in the logs.

2018-06-19 17:06:59.743  INFO 6739 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/myendpoint],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)

Below image shows the output when we invoke our custom actuator endpoint. spring boot actuator custom endpoint

Summary

Spring Boot Actuator is a production ready management and information module. We can easily extend it to add our own APIs and manage our application.

You can download the complete project from our GitHub Repository.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
July 15, 2020

Hello, How can I map my custom liveness checking function to he existing “actuator/health/liveness” endpoint of Spring Boot?

- Barna

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 14, 2020

    Hey, if I want to change health endpoint to heartbeat,how I can do that. Please help

    - Neha Gour

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      January 22, 2020

      "Only “health” and “info” endpoints are exposed without any security, for accessing all other endpoints we need to configure our application for spring security. " -------------- I’m able to access other endpoints without adding spring security.

      - JJose

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        October 17, 2019

        Hi! I uses spring-boot-starter-actuator:1.5.2.RELEASE and the only thing for configuring the base-path (in application.YML) which worked was: ``` management: contextPath: /actuator ``` @see org.springframework.boot.actuate.autoconfigure.ManagementServerProperties#contextPath Otherwise the endpoints of Actuator have been mapped to the application BasePath – in my case ROOT (“/”) Greats!

        - count023

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          June 9, 2019

          Really awesome.

          - MKhan

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            April 29, 2019

            Hi Sir, I need full Spring Boot Tutorials.

            - praveen verma

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              March 5, 2019

              Hi Pankaj, Help me to get entire console log thru endpoint if it is possible using actuator or by other means.

              - xaan

                Try DigitalOcean for free

                Click below to sign up and get $200 of credit to try our products over 60 days!

                Sign up

                Join the Tech Talk
                Success! Thank you! Please check your email for further details.

                Please complete your information!

                Get our biweekly newsletter

                Sign up for Infrastructure as a Newsletter.

                Hollie's Hub for Good

                Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                Become a contributor

                Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                Welcome to the developer cloud

                DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                Learn more
                DigitalOcean Cloud Control Panel