Skip to content

Commit 3b93a82

Browse files
committed
Support relaxed HealthMvcEndpoint status mappings
Update HealthMvcEndpoint so that relaxed names can be used as keys in the `endpoints.health.mapping` property. Fixes gh-2465
1 parent 49583c0 commit 3b93a82

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpoint.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
2626
import org.springframework.boot.actuate.health.Health;
2727
import org.springframework.boot.actuate.health.Status;
28+
import org.springframework.boot.bind.RelaxedNames;
2829
import org.springframework.boot.bind.RelaxedPropertyResolver;
2930
import org.springframework.context.EnvironmentAware;
3031
import org.springframework.core.env.Environment;
@@ -128,13 +129,27 @@ public Object invoke(Principal principal) {
128129
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
129130
}
130131
Health health = getHealth(principal);
131-
HttpStatus status = this.statusMapping.get(health.getStatus().getCode());
132+
HttpStatus status = getStatus(health);
132133
if (status != null) {
133134
return new ResponseEntity<Health>(health, status);
134135
}
135136
return health;
136137
}
137138

139+
private HttpStatus getStatus(Health health) {
140+
String code = health.getStatus().getCode();
141+
if (code != null) {
142+
code = code.toLowerCase().replace("_", "-");
143+
for (String candidate : RelaxedNames.forCamelCase(code)) {
144+
HttpStatus status = this.statusMapping.get(candidate);
145+
if (status != null) {
146+
return status;
147+
}
148+
}
149+
}
150+
return null;
151+
}
152+
138153
private Health getHealth(Principal principal) {
139154
long accessTime = System.currentTimeMillis();
140155
if (isCacheStale(accessTime)) {

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/HealthMvcEndpointTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public void down() {
9191
assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode());
9292
}
9393

94-
@SuppressWarnings("unchecked")
9594
@Test
95+
@SuppressWarnings("unchecked")
9696
public void customMapping() {
9797
given(this.endpoint.invoke()).willReturn(
9898
new Health.Builder().status("OK").build());
@@ -105,6 +105,20 @@ public void customMapping() {
105105
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
106106
}
107107

108+
@Test
109+
@SuppressWarnings("unchecked")
110+
public void customMappingWithRelaxedName() {
111+
given(this.endpoint.invoke()).willReturn(
112+
new Health.Builder().outOfService().build());
113+
this.mvc.setStatusMapping(Collections.singletonMap("out-of-service",
114+
HttpStatus.INTERNAL_SERVER_ERROR));
115+
Object result = this.mvc.invoke(null);
116+
assertTrue(result instanceof ResponseEntity);
117+
ResponseEntity<Health> response = (ResponseEntity<Health>) result;
118+
assertTrue(response.getBody().getStatus().equals(Status.OUT_OF_SERVICE));
119+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
120+
}
121+
108122
@Test
109123
public void secure() {
110124
given(this.endpoint.invoke()).willReturn(

0 commit comments

Comments
 (0)