title: Correct way to do this health check in Java?
body: <p>I am trying to implement a health check for my services in Java. This is using Spring Boot. In this health check, I have multiple services which I call and I need to make sure that the results of each of the service are NULL. The return value of each of the service is then stored into an object which includes the service name and a boolean for whether an error has occurred or not. Currently, I have this setup this way and to me, it looks very ugly. I can't think of another way to implement this.</p>
  
  <pre><code>Public HealthCargo() {
      string service_name;
      boolean error;
  }
  
  @Resource(name = "AService")
  private AService aService;
  
  @Resource(name = "BService")
  private BService bService;
  
  @Resource(name = "CService")
  private CService cService;
  
  @RequestMapping(method = RequestMethod.GET, value = "/healthcheck", produces = {MediaType.APPLICATION_JSON_VALUE})
  @ResponseBody
  public List&lt;HealthCargo&gt; healthcheck() {
      List&lt;HealthCargo&gt; checks = new ArrayList&lt;&gt;();
      boolean error = true;
      SomeCargoA someCargoA = new SomeCargoA();
      SomeCargoB someCargoB = new SomeCargoB();
  
      try {
          someCargoA = aService.fetch("182731");
          if (someCargoA == NULL)
              error = false;
  
      } catch (Exception e) {
      } finally {
          checks.add(New HealthCargo("aService", error));
      }
  
      error = true;
      try {
          someCargoB = bService.fetch("182731");
          if (someCargoB == NULL)
              error = false;
  
      } catch (Exception e) {
      } finally {
          checks.add(New HealthCargo("bService", error));
      }
  
      error = true;    
      .... // Similar logic for service C
  
      return checks;
  }
  </code></pre>
  
  <p>There would be something else that calls this function which will show all the services and their statuses in a list with a green checkmark beside them if the error is false.</p>
  
  <p>I have thought about using asserts, but that would stop the program if there was an error with the assert. I need to store the result of a call as true or false and that depends on whether the object is null or not. Other than using 3 different try catch statements for this, what is another way of doing this? </p>