Add NoHandlerFoundException to DispatcherServlet
Prior to this commit, the DispatcherServlet would send an error response using the HttpServlet API when no Handler was found to handle this request. Now the DispatcherServlet can be configured to throw a NoHandlerFoundException, when the throwExceptionIfNoHandlerFound property is set to "true". Those exceptions can be later on caught by default or custom HandlerExceptionResolvers/ExceptionHandlers. Issue: SPR-10481
This commit is contained in:
committed by
Rossen Stoyanchev
parent
4ef490621f
commit
62d6a43c61
@@ -540,6 +540,20 @@ public class DispatcherServletTests extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public void testThrowExceptionIfNoHandlerFound() throws ServletException, IOException {
|
||||
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
|
||||
complexDispatcherServlet.setContextClass(SimpleWebApplicationContext.class);
|
||||
complexDispatcherServlet.setNamespace("test");
|
||||
complexDispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
|
||||
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
complexDispatcherServlet.service(request, response);
|
||||
assertTrue("correct error code", response.getStatus() == HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
|
||||
public void testCleanupAfterIncludeWithRemove() throws ServletException, IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.validation.BindException;
|
||||
@@ -52,6 +53,7 @@ import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
|
||||
@@ -184,6 +186,15 @@ public class ResponseEntityExceptionHandlerTests {
|
||||
testException(ex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noHandlerFoundException() {
|
||||
ServletServerHttpRequest req = new ServletServerHttpRequest(
|
||||
new MockHttpServletRequest("GET","/resource"));
|
||||
Exception ex = new NoHandlerFoundException(req.getMethod().toString(),
|
||||
req.getServletRequest().getRequestURI(),req.getHeaders());
|
||||
testException(ex);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void controllerAdvice() throws Exception {
|
||||
StaticWebApplicationContext cxt = new StaticWebApplicationContext();
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.Collections;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.ConversionNotSupportedException;
|
||||
import org.springframework.http.server.ServletServerHttpRequest;
|
||||
import org.springframework.tests.sample.beans.TestBean;
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -43,6 +44,7 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.bind.ServletRequestBindingException;
|
||||
import org.springframework.web.multipart.support.MissingServletRequestPartException;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
@@ -171,6 +173,18 @@ public class DefaultHandlerExceptionResolverTests {
|
||||
assertEquals("Invalid status code", 400, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleNoHandlerFoundException() throws Exception {
|
||||
ServletServerHttpRequest req = new ServletServerHttpRequest(
|
||||
new MockHttpServletRequest("GET","/resource"));
|
||||
NoHandlerFoundException ex = new NoHandlerFoundException(req.getMethod().name(),
|
||||
req.getServletRequest().getRequestURI(),req.getHeaders());
|
||||
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
|
||||
assertNotNull("No ModelAndView returned", mav);
|
||||
assertTrue("No Empty ModelAndView returned", mav.isEmpty());
|
||||
assertEquals("Invalid status code", 404, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleConversionNotSupportedException() throws Exception {
|
||||
ConversionNotSupportedException ex =
|
||||
|
||||
Reference in New Issue
Block a user