Add support for global @ExceptionHandler methods
Before this change @ExceptionHandler methods could be located in and apply locally within a controller. The change makes it possible to have such methods applicable globally regardless of the controller that raised the exception. The easiest way to do that is to add them to a class annotated with `@ExceptionResolver`, a new annotation that is also an `@Component` annotation (and therefore works with component scanning). It is also possible to register classes containing `@ExceptionHandler` methods directly with the ExceptionHandlerExceptionResolver. When multiple `@ExceptionResolver` classes are detected, or registered directly, the order in which they're used depends on the the `@Order` annotation (if present) or on the value of the order field (if the Ordered interface is implemented). Issue: SPR-9112
This commit is contained in:
@@ -76,6 +76,15 @@ import java.lang.annotation.Target;
|
||||
* {@link org.springframework.web.servlet.RequestToViewNameTranslator}.
|
||||
* <li>A {@link org.springframework.web.servlet.View} object.
|
||||
* <li>A {@link java.lang.String} value which is interpreted as view name.
|
||||
* <li>{@link ResponseBody @ResponseBody} annotated methods (Servlet-only)
|
||||
* to set the response content. The return value will be converted to the
|
||||
* response stream using
|
||||
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}.
|
||||
* <li>An {@link org.springframework.http.HttpEntity HttpEntity<?>} or
|
||||
* {@link org.springframework.http.ResponseEntity ResponseEntity<?>} object
|
||||
* (Servlet-only) to set response headers and content. The ResponseEntity body
|
||||
* will be converted and written to the response stream using
|
||||
* {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}.
|
||||
* <li><code>void</code> if the method handles the response itself (by
|
||||
* writing the response content directly, declaring an argument of type
|
||||
* {@link javax.servlet.ServletResponse} / {@link javax.servlet.http.HttpServletResponse}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.bind.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* An {@linkplain Component @Component} annotation that indicates the annotated class
|
||||
* contains {@linkplain ExceptionHandler @ExceptionHandler} methods. Such methods
|
||||
* will be used in addition to {@code @ExceptionHandler} methods in
|
||||
* {@code @Controller}-annotated classes.
|
||||
*
|
||||
* <p>In order for the the annotation to detected, an instance of
|
||||
* {@code org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver}
|
||||
* is configured.
|
||||
*
|
||||
* <p>Classes with this annotation may use the {@linkplain Order @Order} annotation
|
||||
* or implement the {@link Ordered} interface to indicate the order in which they
|
||||
* should be used relative to other such annotated components. However, note that
|
||||
* the order is only for components registered through {@code @ExceptionResolver},
|
||||
* i.e. within an
|
||||
* {@code org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 3.2
|
||||
*
|
||||
* @see org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Component
|
||||
public @interface ExceptionResolver {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user