1

I am using SpringBoot Actuator to return health of the app.

public class HealthMonitor implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check();
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    public int check() {
        return 0;
    }

}

I see the below response

{
  "status": "UP",
  "diskSpace": {
    "status": "UP",
    "free": 55020113920,
    "threshold": 10485760
  },
  "db": {
    "status": "UP",
    "database": "Oracle",
    "hello": "Hello"
  }
}

I want to return a response similar to below

{status: "Healthy"}

Is there a way to do it?

Punter Vicky
  • 12,516
  • 39
  • 134
  • 241
  • Instead of returning `Health` instance return your custom response after checking `errorCode` – 11thdimension Aug 15 '16 at 18:29
  • Should I not implement HealthIndicator in that case? Is there a way return a custom message like what I mentioned using Health instance? – Punter Vicky Aug 15 '16 at 18:44
  • I thought `Health` was some normal class. I tried it myself, only way I can think of now is to protect this service from external use and create a custom service something like `/customHealth` and use this `/health` service on the server to obtain the status information in the custom service. – 11thdimension Aug 15 '16 at 19:29
  • Thanks @11thdimension. please post it as an answer and I'll accept it – Punter Vicky Aug 16 '16 at 15:58
  • It was just a suggestion. If I do implement this then I will post it as an answer, thanks for considering. If you implement it then post that as an answer that will help others. – 11thdimension Aug 16 '16 at 16:00
  • Yes , I will post after I implement it. Thank You. – Punter Vicky Aug 16 '16 at 16:01

3 Answers3

0

I don't think you're going to be able to persuade the supplied Spring Boot health endpoint to display in a dramatically different format.

Most of the Spring source code is very clear, and this is no exception. Look at the source for org.springframework.boot.actuate.endpoint.HealthEndpoint and you'll see it has an invoke() method that returns a Health. The JSON you are seeing is Jackson's marshalling of that object.

You could, however, create your own endpoint, extending AbstractEndpoint in exactly the same way as HealthEndpoint does, returning whatever class of your own you like.

You could even make it delegate to the HealthEndpoint bean:

 public MyHealthEndpoint(HealthEndpoint delegate) {
     this.delegate = delegate;
 }

 @Override
 public MyHealth invoke() {
      Health health = delegate.invoke();
      return new MyHealth(health.getStatus());
 }
slim
  • 36,139
  • 10
  • 83
  • 117
0

To customize your Status message there is one method named: withDetail(). So when you are writing return Health.up().build(); just replace this with

return Health.up().withDetail("My Application Status","Healthy")

So this will give

{My Application Status: "Healthy"}
Syscall
  • 16,959
  • 9
  • 22
  • 41
Anju rawat
  • 71
  • 2
-1

There is a lot of flexibility with Spring Boot and its health indicators.

First, depending on the complexity of your service, you may not need the HealthIndicator you describe above. Out of the box, Spring Boot will return status' based upon your spring configurations. This includes databases (both SQL and some noSQL), Mail, JMS and others. This is described in the latest Spring Boot documents in section 47.6.1

The easiest way to return what you want, with or without the HealthIndicator class, is to set a property in your application.properties:

endpoints.health.sensitive=true

This may only apply to the later versions (after 1.4?) of Spring Boot.

EJoshuaS - Reinstate Monica
  • 10,460
  • 46
  • 38
  • 64