From 5214bd309370e328ee874a50144af2e00787f7fe Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 6 Dec 2022 12:18:55 +0000 Subject: [PATCH] Fix issue with getHeaders in NoHandlerFoundException Closes gh-29626 --- .../web/servlet/NoHandlerFoundException.java | 21 ++++++++++++++++--- .../ResponseEntityExceptionHandlerTests.java | 8 ++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/NoHandlerFoundException.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/NoHandlerFoundException.java index e7d45c4e2d40..99362952fe4b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/NoHandlerFoundException.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/NoHandlerFoundException.java @@ -44,7 +44,7 @@ public class NoHandlerFoundException extends ServletException implements ErrorRe private final String requestURL; - private final HttpHeaders headers; + private final HttpHeaders requestHeaders; private final ProblemDetail body; @@ -59,7 +59,7 @@ public NoHandlerFoundException(String httpMethod, String requestURL, HttpHeaders super("No endpoint " + httpMethod + " " + requestURL + "."); this.httpMethod = httpMethod; this.requestURL = requestURL; - this.headers = headers; + this.requestHeaders = headers; this.body = ProblemDetail.forStatusAndDetail(getStatusCode(), getMessage()); } @@ -76,8 +76,23 @@ public String getRequestURL() { return this.requestURL; } + /** + * Return headers to use for the response. + *

Note: As of 6.0 this method overlaps with + * {@link ErrorResponse#getHeaders()} and therefore no longer returns request + * headers. Use {@link #getRequestHeaders()} instead for request headers. + */ + @Override public HttpHeaders getHeaders() { - return this.headers; + return ErrorResponse.super.getHeaders(); + } + + /** + * Return the headers of the request. + * @since 6.0.3 + */ + public HttpHeaders getRequestHeaders() { + return this.requestHeaders; } @Override diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java index ad8e1187eb73..12529ef528ad 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ResponseEntityExceptionHandlerTests.java @@ -253,7 +253,13 @@ public void bindException() { @Test public void noHandlerFoundException() { - testException(new NoHandlerFoundException("GET", "/resource", HttpHeaders.EMPTY)); + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // gh-29626 + + ResponseEntity responseEntity = + testException(new NoHandlerFoundException("GET", "/resource", requestHeaders)); + + assertThat(responseEntity.getHeaders()).isEmpty(); } @Test