How to Restart Spring Boot Automatically using Actuator

This guide will show you how easily we can manage our running Spring Boot Application and how to restart Spring Boot automatically using Actuator. Spring Actuator lets you manage your Spring Application and get the information easily.

For this guide, we will be using our Simple Spring Boot Application. We will be adding the dependencies for Spring Actuator and Spring Cloud then we will expose the endpoints. Then we will test the actuator by visiting its URL and restart spring boot automatically by using CURL.

Add Spring Actuator and Spring Cloud Dependencies

Let’s edit the pom.xml and add the following dependencies.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

If you are using Spring Boot, you don’t need to add the version for Spring Actuator as it will be relative to your Spring Boot parent version. Basically, Spring Actuator already gives you a lot of management endpoints that can be used. But if we want to restart our Spring Boot Application remotely, then we need the Spring Cloud dependency as it has the /restart endpoint that we need.

Enable and Exposed the Spring Boot Restart Endpoint

In your application.properties file, add the following properties. This will enable the /restart endpoint that we can call to restart our application.

management.endpoint.restart.enabled=true
management.endpoints.web.exposure.include=restart,health

Here, we defined that restart endpoint should be enabled and we expose it along with the health endpoint. Visit the official Spring Actuator Endpoints Documentation and learn more about the different endpoints that you can enable. Here’s our complete application.properties file:

server.servlet.contextPath=/javapointers
server.port=8085

#actuator
management.endpoint.restart.enabled=true
management.endpoints.web.exposure.include=restart,health

Ways to Restart Spring Boot Automatically

The very basic way to restart the Spring Boot Application is by sending a POST request to the /restart endpoint. Let’s try that using CURL. After running your Spring Boot Application, open a command prompt and type:

curl -X POST http://localhost:{PORT}{CONTEXTPATH}/actuator/restart

Since we are using port 8085 and /javapointers context path, we will then send a POST request to http://localhost:8085/javapointers/actuator/restart URL. You should see an output Restarting… in JSON format similar to below:

spring-boot restart automatically using actuator.

We can also restart our spring boot programmatically. This is useful for example if we wanted to restart the spring boot application in a separate program. Here’s an example.

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

public class Test {

    public static void main(String... args) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<String> entity = new HttpEntity<>(null, headers);

        RestTemplateBuilder builder = new RestTemplateBuilder();
        String response = builder.build()
                .postForObject("http://localhost:8085/javapointers/actuator/restart", entity, String.class);

        System.out.println(response);
    }
}

That’s it. We hope you learned how to restart your Spring Boot Application Automatically using Spring Actuator and Spring Cloud. Let us know in the comments if you have any questions.

Share this tutorial!