private void setupDefaultStatusMapping() { addStatusMapping(Status.DOWN, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE); addStatusMapping(Status.OUT_OF_SERVICE, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE); }
/** * Create a new instance. */ public HealthStatusHttpMapper() { setupDefaultStatusMapping(); }
@Bean @ConditionalOnMissingBean public HealthStatusHttpMapper createHealthStatusHttpMapper( HealthIndicatorProperties healthIndicatorProperties) { HealthStatusHttpMapper statusHttpMapper = new HealthStatusHttpMapper(); if (healthIndicatorProperties.getHttpMapping() != null) { statusHttpMapper.addStatusMapping(healthIndicatorProperties.getHttpMapping()); } return statusHttpMapper; }
private WebEndpointResponse<Health> createWebEndpointResponse(Health health) { Integer status = this.statusHttpMapper.mapStatus(health.getStatus()); return new WebEndpointResponse<>(health, status); }
/** * Map the specified {@link Status} to an HTTP status code. * @param status the health {@link Status} * @return the corresponding HTTP status code */ public int mapStatus(Status status) { String code = getUniformValue(status.getCode()); if (code != null) { return this.statusMapping.entrySet().stream() .filter((entry) -> code.equals(getUniformValue(entry.getKey()))) .map(Map.Entry::getValue).findFirst() .orElse(WebEndpointResponse.STATUS_OK); } return WebEndpointResponse.STATUS_OK; }
@GetMapping("/health") public ResponseEntity<Map> getHealth() { Health health = healthEndpoint.health(); Map body = new HashMap(); body.put("status", health.getStatus().getCode()); body.put("zipkin", health.getDetails().get("zipkin")); return ResponseEntity.status(statusMapper.mapStatus(health.getStatus())).body(body); }
/** * Add a status mapping to the existing set. * @param status the status to map * @param httpStatus the http status */ public void addStatusMapping(Status status, Integer httpStatus) { Assert.notNull(status, "Status must not be null"); Assert.notNull(httpStatus, "HttpStatus must not be null"); addStatusMapping(status.getCode(), httpStatus); }