File tree Expand file tree Collapse file tree 2 files changed +31
-2
lines changed
main/java/org/springframework/boot/actuate/endpoint/mvc
test/java/org/springframework/boot/actuate/endpoint/mvc Expand file tree Collapse file tree 2 files changed +31
-2
lines changed Original file line number Diff line number Diff line change 25
25
import org .springframework .boot .actuate .endpoint .HealthEndpoint ;
26
26
import org .springframework .boot .actuate .health .Health ;
27
27
import org .springframework .boot .actuate .health .Status ;
28
+ import org .springframework .boot .bind .RelaxedNames ;
28
29
import org .springframework .boot .bind .RelaxedPropertyResolver ;
29
30
import org .springframework .context .EnvironmentAware ;
30
31
import org .springframework .core .env .Environment ;
@@ -128,13 +129,27 @@ public Object invoke(Principal principal) {
128
129
"message" , "This endpoint is disabled" ), HttpStatus .NOT_FOUND );
129
130
}
130
131
Health health = getHealth (principal );
131
- HttpStatus status = this . statusMapping . get ( health . getStatus (). getCode () );
132
+ HttpStatus status = getStatus (health );
132
133
if (status != null ) {
133
134
return new ResponseEntity <Health >(health , status );
134
135
}
135
136
return health ;
136
137
}
137
138
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
+
138
153
private Health getHealth (Principal principal ) {
139
154
long accessTime = System .currentTimeMillis ();
140
155
if (isCacheStale (accessTime )) {
Original file line number Diff line number Diff line change @@ -91,8 +91,8 @@ public void down() {
91
91
assertEquals (HttpStatus .SERVICE_UNAVAILABLE , response .getStatusCode ());
92
92
}
93
93
94
- @ SuppressWarnings ("unchecked" )
95
94
@ Test
95
+ @ SuppressWarnings ("unchecked" )
96
96
public void customMapping () {
97
97
given (this .endpoint .invoke ()).willReturn (
98
98
new Health .Builder ().status ("OK" ).build ());
@@ -105,6 +105,20 @@ public void customMapping() {
105
105
assertEquals (HttpStatus .INTERNAL_SERVER_ERROR , response .getStatusCode ());
106
106
}
107
107
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
+
108
122
@ Test
109
123
public void secure () {
110
124
given (this .endpoint .invoke ()).willReturn (
You can’t perform that action at this time.
0 commit comments