diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsWebEndpointExtension.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsWebEndpointExtension.java index 56892587164..75f1869a5df 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsWebEndpointExtension.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsWebEndpointExtension.java @@ -22,7 +22,6 @@ import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDes import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse; import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension; -import org.springframework.http.HttpStatus; /** * {@link WebEndpointExtension} for the {@link AuditEventsEndpoint}. @@ -43,7 +42,7 @@ public class AuditEventsWebEndpointExtension { public WebEndpointResponse eventsWithPrincipalDateAfterAndType( String principal, Date after, String type) { if (after == null) { - return new WebEndpointResponse<>(HttpStatus.BAD_REQUEST.value()); + return new WebEndpointResponse<>(WebEndpointResponse.BAD_REQUEST_STATUS); } AuditEventsDescriptor auditEvents = this.delegate .eventsWithPrincipalDateAfterAndType(principal, after, type); diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebEndpointResponse.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebEndpointResponse.java index 37c2584f0cf..cfd6482dc47 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebEndpointResponse.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebEndpointResponse.java @@ -26,10 +26,36 @@ import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExten * @param the type of the response body * @author Stephane Nicoll * @author Andy Wilkinson + * @author Vedran Pavic * @since 2.0.0 */ public final class WebEndpointResponse { + /** + * {@code 200 OK}. + */ + public static final int OK_STATUS = 200; + + /** + * {@code 400 Bad Request}. + */ + public static final int BAD_REQUEST_STATUS = 400; + + /** + * {@code 429 Too Many Requests}. + */ + public static final int TOO_MANY_REQUESTS_STATUS = 429; + + /** + * {@code 500 Internal Server Error}. + */ + public static final int INTERNAL_SERVER_ERROR_STATUS = 500; + + /** + * {@code 503 Service Unavailable}. + */ + public static final int SERVICE_UNAVAILABLE_STATUS = 503; + private final T body; private final int status; @@ -56,7 +82,7 @@ public final class WebEndpointResponse { * @param body the body */ public WebEndpointResponse(T body) { - this(body, 200); + this(body, OK_STATUS); } /** diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthStatusHttpMapper.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthStatusHttpMapper.java index 7e543751dcc..d7e41f5264c 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthStatusHttpMapper.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthStatusHttpMapper.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse; import org.springframework.util.Assert; /** @@ -40,8 +41,9 @@ public class HealthStatusHttpMapper { } private void setupDefaultStatusMapping() { - addStatusMapping(Status.DOWN, 503); - addStatusMapping(Status.OUT_OF_SERVICE, 503); + addStatusMapping(Status.DOWN, WebEndpointResponse.SERVICE_UNAVAILABLE_STATUS); + addStatusMapping(Status.OUT_OF_SERVICE, + WebEndpointResponse.SERVICE_UNAVAILABLE_STATUS); } /** @@ -102,9 +104,10 @@ public class HealthStatusHttpMapper { if (code != null) { return this.statusMapping.keySet().stream() .filter((key) -> code.equals(getUniformValue(key))) - .map(this.statusMapping::get).findFirst().orElse(200); + .map(this.statusMapping::get).findFirst() + .orElse(WebEndpointResponse.OK_STATUS); } - return 200; + return WebEndpointResponse.OK_STATUS; } private String getUniformValue(String code) { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java index ef3c9393b4b..2dfb081f997 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java @@ -42,7 +42,6 @@ import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; -import org.springframework.http.HttpStatus; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; @@ -89,12 +88,14 @@ public class HeapDumpWebEndpoint { Thread.currentThread().interrupt(); } catch (IOException ex) { - return new WebEndpointResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value()); + return new WebEndpointResponse<>( + WebEndpointResponse.INTERNAL_SERVER_ERROR_STATUS); } catch (HeapDumperUnavailableException ex) { - return new WebEndpointResponse<>(HttpStatus.SERVICE_UNAVAILABLE.value()); + return new WebEndpointResponse<>( + WebEndpointResponse.SERVICE_UNAVAILABLE_STATUS); } - return new WebEndpointResponse<>(HttpStatus.TOO_MANY_REQUESTS.value()); + return new WebEndpointResponse<>(WebEndpointResponse.TOO_MANY_REQUESTS_STATUS); } private Resource dumpHeap(boolean live) throws IOException, InterruptedException {