Add "excludedExceptions" to SimpleUrlHandlerMapping

The new property can be used to ignore specific exceptions that may
otherwise be matched by the "exceptionMappings" property or resolved
through the defaultErrorView.

Issue: SPR-5193
master
Rossen Stoyanchev 13 years ago
parent 1167155182
commit fbb2103e4d
  1. 30
      spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolver.java
  2. 18
      spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolverTests.java
  3. 1
      src/dist/changelog.txt

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -36,6 +36,7 @@ import org.springframework.web.util.WebUtils;
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Rossen Stoyanchev
* @since 22.11.2003 * @since 22.11.2003
* @see org.springframework.web.servlet.DispatcherServlet * @see org.springframework.web.servlet.DispatcherServlet
*/ */
@ -46,6 +47,8 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso
private Properties exceptionMappings; private Properties exceptionMappings;
private Class<?>[] excludedExceptions;
private String defaultErrorView; private String defaultErrorView;
private Integer defaultStatusCode; private Integer defaultStatusCode;
@ -73,6 +76,16 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso
this.exceptionMappings = mappings; this.exceptionMappings = mappings;
} }
/**
* Set one or more exceptions to be excluded from the exception mappings.
* Excluded exceptions are checked first and if one of them equals the actual
* exception, the exception will remain unresolved.
* @param excludedExceptions one or more excluded exception types
*/
public void setExcludedExceptions(Class<?>... excludedExceptions) {
this.excludedExceptions = excludedExceptions;
}
/** /**
* Set the name of the default error view. This view will be returned if no specific mapping was found. <p>Default is * Set the name of the default error view. This view will be returned if no specific mapping was found. <p>Default is
* none. * none.
@ -172,14 +185,23 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso
} }
/** /**
* Determine the view name for the given exception, searching the {@link #setExceptionMappings "exceptionMappings"}, * Determine the view name for the given exception, first checking against the
* using the {@link #setDefaultErrorView "defaultErrorView"} as fallback. * {@link #setExcludedExceptions(Class[]) "excludedExecptions"}, then searching the
* {@link #setExceptionMappings "exceptionMappings"}, and finally using the
* {@link #setDefaultErrorView "defaultErrorView"} as a fallback.
* @param ex the exception that got thrown during handler execution * @param ex the exception that got thrown during handler execution
* @param request current HTTP request (useful for obtaining metadata) * @param request current HTTP request (useful for obtaining metadata)
* @return the resolved view name, or <code>null</code> if none found * @return the resolved view name, or <code>null</code> if excluded or none found
*/ */
protected String determineViewName(Exception ex, HttpServletRequest request) { protected String determineViewName(Exception ex, HttpServletRequest request) {
String viewName = null; String viewName = null;
if (this.excludedExceptions != null) {
for (Class<?> excludedEx : this.excludedExceptions) {
if (excludedEx.equals(ex.getClass())) {
return null;
}
}
}
// Check for specific exception mappings. // Check for specific exception mappings.
if (this.exceptionMappings != null) { if (this.exceptionMappings != null) {
viewName = findMatchingViewName(this.exceptionMappings, ex); viewName = findMatchingViewName(this.exceptionMappings, ex);

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2012 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,16 @@
package org.springframework.web.servlet.handler; package org.springframework.web.servlet.handler;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Collections; import java.util.Collections;
import java.util.Properties; import java.util.Properties;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
@ -196,6 +198,16 @@ public class SimpleMappingExceptionResolverTests {
assertNull(mav); assertNull(mav);
} }
@Test
public void simpleExceptionMappingWithExclusion() {
Properties props = new Properties();
props.setProperty("Exception", "error");
exceptionResolver.setExceptionMappings(props);
exceptionResolver.setExcludedExceptions(IllegalArgumentException.class);
ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, new IllegalArgumentException());
assertNull(mav);
}
@Test @Test
public void missingExceptionInMapping() { public void missingExceptionInMapping() {
Properties props = new Properties(); Properties props = new Properties();

@ -23,6 +23,7 @@ Changes in version 3.2 M1
* Prevent further writing to the response when @ResponseStatus contains a reason * Prevent further writing to the response when @ResponseStatus contains a reason
* Deprecate HttpStatus codes 419, 420, 421 * Deprecate HttpStatus codes 419, 420, 421
* support access to all URI vars via @PathVariable Map<String, String> * support access to all URI vars via @PathVariable Map<String, String>
* add "excludedExceptions" property to SimpleUrlHandlerMapping
Changes in version 3.1.1 (2012-02-16) Changes in version 3.1.1 (2012-02-16)
------------------------------------- -------------------------------------

Loading…
Cancel
Save