Skip to content

Commit

Permalink
Allow actuator endpoint to have responses with custom HTTP statuses
Browse files Browse the repository at this point in the history
Fixes gh-24123
  • Loading branch information
wilkinsona committed Nov 20, 2020
1 parent fbf4c8c commit 12f2529
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Expand Up @@ -361,8 +361,7 @@ private ResponseEntity<Object> toResponseEntity(Object response) {
return new ResponseEntity<>(response, HttpStatus.OK);
}
WebEndpointResponse<?> webEndpointResponse = (WebEndpointResponse<?>) response;
return new ResponseEntity<>(webEndpointResponse.getBody(),
HttpStatus.valueOf(webEndpointResponse.getStatus()));
return ResponseEntity.status(webEndpointResponse.getStatus()).body(webEndpointResponse.getBody());
}

@Override
Expand Down
Expand Up @@ -367,7 +367,7 @@ private Object handleResult(Object result, HttpMethod httpMethod) {
return result;
}
WebEndpointResponse<?> response = (WebEndpointResponse<?>) result;
return new ResponseEntity<Object>(response.getBody(), HttpStatus.valueOf(response.getStatus()));
return ResponseEntity.status(response.getStatus()).body(response.getBody());
}

}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -379,6 +379,12 @@ void userInRoleReturnsTrueWhenUserIsInRole() {
.expectStatus().isOk().expectBody(String.class).isEqualTo("ACTUATOR: true"));
}

@Test
void endpointCanProduceAResponseWithACustomStatus() {
load((context) -> context.register(CustomResponseStatusEndpointConfiguration.class),
(client) -> client.get().uri("/customstatus").exchange().expectStatus().isEqualTo(234));
}

protected abstract int getPort(T context);

protected void validateErrorBody(WebTestClient.BodyContentSpec body, HttpStatus status, String path,
Expand Down Expand Up @@ -621,6 +627,17 @@ UserInRoleEndpoint userInRoleEndpoint() {

}

@Configuration(proxyBeanMethods = false)
@Import(BaseConfiguration.class)
static class CustomResponseStatusEndpointConfiguration {

@Bean
CustomResponseStatusEndpoint customResponseStatusEndpoint() {
return new CustomResponseStatusEndpoint();
}

}

@Endpoint(id = "test")
static class TestEndpoint {

Expand Down Expand Up @@ -847,6 +864,16 @@ String read(SecurityContext securityContext, String role) {

}

@Endpoint(id = "customstatus")
static class CustomResponseStatusEndpoint {

@ReadOperation
WebEndpointResponse<String> read() {
return new WebEndpointResponse<>("Custom status", 234);
}

}

interface EndpointDelegate {

void write();
Expand Down

0 comments on commit 12f2529

Please sign in to comment.