/api/health-check
curl --location 'http://127.0.0.1:8080/api/v1/health-check'{
"status": "UP"
}{
"status": "DOWN"
}| package com.example.irfan.rest; | |
| import org.springframework.boot.actuate.health.HealthEndpoint; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.web.bind.annotation.GetMapping; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.RestController; | |
| @RestController | |
| @RequestMapping(value = "/api/health-check", produces = {MediaType.APPLICATION_JSON_VALUE}) | |
| public class HealthCheckController { | |
| private final HealthEndpoint healthEndpoint; | |
| public HealthCheckController(HealthEndpoint healthEndpoint) { | |
| this.healthEndpoint = healthEndpoint; | |
| } | |
| @GetMapping(value = "/ping") | |
| public ResponseEntity<String> ping() { | |
| return ResponseEntity.ok("{\"status\":\"OK\"}"); | |
| } | |
| @GetMapping(value = {"", "/"}) | |
| public ResponseEntity<String> check() { | |
| String health = healthEndpoint.health().getStatus().getCode(); | |
| return ResponseEntity.ok("{\"status\":\"" + health + "\"}"); | |
| } | |
| } |
| package com.example.irfan.health; | |
| import org.springframework.boot.actuate.health.Health; | |
| import org.springframework.boot.actuate.health.HealthIndicator; | |
| import org.springframework.stereotype.Component; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| @Component | |
| public class ThirdPartyHealthCheck implements HealthIndicator { | |
| @Override | |
| public Health health() { | |
| Map<String, Object> details = new HashMap<>(); | |
| details.put("chance", "something"); | |
| // return Health.down().withDetails(details).build(); // in case of failure | |
| return Health.up().withDetails(details).build(); | |
| } | |
| } |