Skip to content

Commit 8a1a23a

Browse files
committed
SPR-7060 - @ResponseStatus: The reason value is not used.
1 parent 580dc8e commit 8a1a23a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,17 @@ public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Obj
814814
ResponseStatus responseStatusAnn = AnnotationUtils.findAnnotation(handlerMethod, ResponseStatus.class);
815815
if (responseStatusAnn != null) {
816816
HttpStatus responseStatus = responseStatusAnn.value();
817+
String reason = responseStatusAnn.reason();
818+
if (!StringUtils.hasText(reason)) {
819+
webRequest.getResponse().setStatus(responseStatus.value());
820+
}
821+
else {
822+
webRequest.getResponse().sendError(responseStatus.value(), reason);
823+
}
824+
817825
// to be picked up by the RedirectView
818826
webRequest.getRequest().setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, responseStatus);
819-
webRequest.getResponse().setStatus(responseStatus.value());
827+
820828
responseArgumentUsed = true;
821829
}
822830

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ public void responseStatus() throws ServletException, IOException {
13071307
servlet.service(request, response);
13081308
assertEquals("something", response.getContentAsString());
13091309
assertEquals(201, response.getStatus());
1310+
assertEquals("It's alive!", response.getErrorMessage());
13101311
}
13111312

13121313
@Test
@@ -2382,7 +2383,7 @@ public void handleXml(Writer writer) throws IOException {
23822383
public static class ResponseStatusController {
23832384

23842385
@RequestMapping("/something")
2385-
@ResponseStatus(HttpStatus.CREATED)
2386+
@ResponseStatus(value = HttpStatus.CREATED, reason = "It's alive!")
23862387
public void handle(Writer writer) throws IOException {
23872388
writer.write("something");
23882389
}

0 commit comments

Comments
 (0)