diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java index 7402df864b9..1dec1249119 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java @@ -25,6 +25,7 @@ import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.actuate.endpoint.HealthEndpoint; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Status; +import org.springframework.boot.bind.RelaxedNames; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; @@ -128,13 +129,27 @@ public class HealthMvcEndpoint implements MvcEndpoint, EnvironmentAware { "message", "This endpoint is disabled"), HttpStatus.NOT_FOUND); } Health health = getHealth(principal); - HttpStatus status = this.statusMapping.get(health.getStatus().getCode()); + HttpStatus status = getStatus(health); if (status != null) { return new ResponseEntity(health, status); } return health; } + private HttpStatus getStatus(Health health) { + String code = health.getStatus().getCode(); + if (code != null) { + code = code.toLowerCase().replace("_", "-"); + for (String candidate : RelaxedNames.forCamelCase(code)) { + HttpStatus status = this.statusMapping.get(candidate); + if (status != null) { + return status; + } + } + } + return null; + } + private Health getHealth(Principal principal) { long accessTime = System.currentTimeMillis(); if (isCacheStale(accessTime)) { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java index 9725be1d357..f2bd062518f 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java @@ -91,8 +91,8 @@ public class HealthMvcEndpointTests { assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); } - @SuppressWarnings("unchecked") @Test + @SuppressWarnings("unchecked") public void customMapping() { given(this.endpoint.invoke()).willReturn( new Health.Builder().status("OK").build()); @@ -105,6 +105,20 @@ public class HealthMvcEndpointTests { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); } + @Test + @SuppressWarnings("unchecked") + public void customMappingWithRelaxedName() { + given(this.endpoint.invoke()).willReturn( + new Health.Builder().outOfService().build()); + this.mvc.setStatusMapping(Collections.singletonMap("out-of-service", + HttpStatus.INTERNAL_SERVER_ERROR)); + Object result = this.mvc.invoke(null); + assertTrue(result instanceof ResponseEntity); + ResponseEntity response = (ResponseEntity) result; + assertTrue(response.getBody().getStatus().equals(Status.OUT_OF_SERVICE)); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + @Test public void secure() { given(this.endpoint.invoke()).willReturn(