Skip to content

Commit 92beba1

Browse files
committed
Merge pull request #10350 from vpavic:gh-10348
* pr/10350: Polish "Remove usage of `HttpStatus` in Web Endpoints" Remove usage of `HttpStatus` in Web Endpoints
2 parents 326290b + f43aa94 commit 92beba1

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/audit/AuditEventsWebEndpointExtension.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
2323
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
2424
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
25-
import org.springframework.http.HttpStatus;
2625

2726
/**
2827
* {@link WebEndpointExtension} for the {@link AuditEventsEndpoint}.
@@ -43,7 +42,7 @@ public AuditEventsWebEndpointExtension(AuditEventsEndpoint delegate) {
4342
public WebEndpointResponse<AuditEventsDescriptor> eventsWithPrincipalDateAfterAndType(
4443
String principal, Date after, String type) {
4544
if (after == null) {
46-
return new WebEndpointResponse<>(HttpStatus.BAD_REQUEST.value());
45+
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST);
4746
}
4847
AuditEventsDescriptor auditEvents = this.delegate
4948
.eventsWithPrincipalDateAfterAndType(principal, after, type);

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebEndpointResponse.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,41 @@
2626
* @param <T> the type of the response body
2727
* @author Stephane Nicoll
2828
* @author Andy Wilkinson
29+
* @author Vedran Pavic
2930
* @since 2.0.0
3031
*/
3132
public final class WebEndpointResponse<T> {
3233

34+
/**
35+
* {@code 200 OK}.
36+
*/
37+
public static final int STATUS_OK = 200;
38+
39+
/**
40+
* {@code 400 Bad Request}.
41+
*/
42+
public static final int STATUS_BAD_REQUEST = 400;
43+
44+
/**
45+
* {@code 404 Not Found}.
46+
*/
47+
public static final int STATUS_NOT_FOUND = 404;
48+
49+
/**
50+
* {@code 429 Too Many Requests}.
51+
*/
52+
public static final int STATUS_TOO_MANY_REQUESTS = 429;
53+
54+
/**
55+
* {@code 500 Internal Server Error}.
56+
*/
57+
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
58+
59+
/**
60+
* {@code 503 Service Unavailable}.
61+
*/
62+
public static final int STATUS_SERVICE_UNAVAILABLE = 503;
63+
3364
private final T body;
3465

3566
private final int status;
@@ -56,7 +87,7 @@ public WebEndpointResponse(int status) {
5687
* @param body the body
5788
*/
5889
public WebEndpointResponse(T body) {
59-
this(body, 200);
90+
this(body, STATUS_OK);
6091
}
6192

6293
/**

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthStatusHttpMapper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.Map;
2222

23+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
2324
import org.springframework.util.Assert;
2425

2526
/**
@@ -40,8 +41,9 @@ public HealthStatusHttpMapper() {
4041
}
4142

4243
private void setupDefaultStatusMapping() {
43-
addStatusMapping(Status.DOWN, 503);
44-
addStatusMapping(Status.OUT_OF_SERVICE, 503);
44+
addStatusMapping(Status.DOWN, WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
45+
addStatusMapping(Status.OUT_OF_SERVICE,
46+
WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
4547
}
4648

4749
/**
@@ -102,9 +104,10 @@ public int mapStatus(Status status) {
102104
if (code != null) {
103105
return this.statusMapping.keySet().stream()
104106
.filter((key) -> code.equals(getUniformValue(key)))
105-
.map(this.statusMapping::get).findFirst().orElse(200);
107+
.map(this.statusMapping::get).findFirst()
108+
.orElse(WebEndpointResponse.STATUS_OK);
106109
}
107-
return 200;
110+
return WebEndpointResponse.STATUS_OK;
108111
}
109112

110113
private String getUniformValue(String code) {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
4343
import org.springframework.core.io.FileSystemResource;
4444
import org.springframework.core.io.Resource;
45-
import org.springframework.http.HttpStatus;
4645
import org.springframework.util.ClassUtils;
4746
import org.springframework.util.ReflectionUtils;
4847

@@ -89,12 +88,14 @@ public WebEndpointResponse<Resource> heapDump(Boolean live) {
8988
Thread.currentThread().interrupt();
9089
}
9190
catch (IOException ex) {
92-
return new WebEndpointResponse<>(HttpStatus.INTERNAL_SERVER_ERROR.value());
91+
return new WebEndpointResponse<>(
92+
WebEndpointResponse.STATUS_INTERNAL_SERVER_ERROR);
9393
}
9494
catch (HeapDumperUnavailableException ex) {
95-
return new WebEndpointResponse<>(HttpStatus.SERVICE_UNAVAILABLE.value());
95+
return new WebEndpointResponse<>(
96+
WebEndpointResponse.STATUS_SERVICE_UNAVAILABLE);
9697
}
97-
return new WebEndpointResponse<>(HttpStatus.TOO_MANY_REQUESTS.value());
98+
return new WebEndpointResponse<>(WebEndpointResponse.STATUS_TOO_MANY_REQUESTS);
9899
}
99100

100101
private Resource dumpHeap(boolean live) throws IOException, InterruptedException {

0 commit comments

Comments
 (0)