From e843bcc725d555ed51d30e95661b2041f5a5a797 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 14 May 2015 06:19:47 -0400 Subject: [PATCH] Do not log headers in exception message Issue: SPR-12984 --- .../web/servlet/NoHandlerFoundException.java | 9 +++++---- .../web/servlet/DispatcherServletTests.java | 11 +++++++++++ 2 files changed, 16 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 d6d5ca8b0b..63f83c1c22 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 @@ -23,9 +23,10 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpHeaders; /** - * Exception to be thrown if DispatcherServlet is unable to determine a corresponding - * handler for an incoming HTTP request. The DispatcherServlet throws this exception - * only if its "throwExceptionIfNoHandlerFound" property is set to "true". + * By default when the DispatcherServlet can't find a handler for a request it + * sends a 404 response. However if its property "throwExceptionIfNoHandlerFound" + * is set to {@code true} this exception is raised and may be handled with + * a configured HandlerExceptionResolver. * * @author Brian Clozel * @since 4.0 @@ -49,7 +50,7 @@ public class NoHandlerFoundException extends ServletException { * @param headers the HTTP request headers */ public NoHandlerFoundException(String httpMethod, String requestURL, HttpHeaders headers) { - super("No handler found for " + httpMethod + " " + requestURL + ", headers=" + headers); + super("No handler found for " + httpMethod + " " + requestURL); this.httpMethod = httpMethod; this.requestURL = requestURL; this.headers = headers; diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java index 6f3a4fff93..ca9b2894cb 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/DispatcherServletTests.java @@ -34,6 +34,7 @@ import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.DummyEnvironment; +import org.springframework.http.HttpHeaders; import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockServletConfig; @@ -574,6 +575,16 @@ public class DispatcherServletTests extends TestCase { assertTrue("correct error code", response.getStatus() == HttpServletResponse.SC_NOT_FOUND); } + // SPR-12984 + + public void testNoHandlerFoundExceptionMessage() { + HttpHeaders headers = new HttpHeaders(); + headers.add("foo", "bar"); + NoHandlerFoundException ex = new NoHandlerFoundException("GET", "/foo", headers); + assertTrue(!ex.getMessage().contains("bar")); + assertTrue(!ex.toString().contains("bar")); + } + public void testCleanupAfterIncludeWithRemove() throws ServletException, IOException { MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do"); MockHttpServletResponse response = new MockHttpServletResponse();