Spring Boot Actuator: is there individual status as plain text?

I am trying to integrate Spring Boot Actuator with existing infrastructures of my companies. To do this, I need to set up a status message. For example, if the application is running and working correctly, I need to return 200 and the body of the plain text "HAPPY" from the endpoint of the health drive.

Is such a setting possible? Since the Status class is final, I cannot extend it, but I think it will work.

+4
source share
1 answer

Spring Boot uses HealthAggregatorto combine all statuses from individual health indicators into a single whole for the entire application. You can attach a custom aggregator that delegates the default boot aggregator OrderedHealthAggregator, and then maps UPto HAPPY:

@Bean
public HealthAggregator healthAggregator() {
    return new HappyHealthAggregator(new OrderedHealthAggregator());
}

static class HappyHealthAggregator implements HealthAggregator {

    private final HealthAggregator delegate;

    HappyHealthAggregator(HealthAggregator delegate) {
        this.delegate = delegate;
    }

    @Override
    public Health aggregate(Map<String, Health> healths) {
        Health result = this.delegate.aggregate(healths);
        if (result.getStatus() == Status.UP) {
            return new Health.Builder(new Status("HAPPY"), result.getDetails())
                    .build();
        }
        return result;
    }

}

If you want to completely control the response format, you will need to write your own MVC endpoint implementation. You can use an existing class HealthMvcEndpointin Spring Boot as a superclass and override its method invoke.

+1
source

Source: https://habr.com/ru/post/1667196/


All Articles