@Target(value=METHOD)
@Retention(value=RUNTIME)
@Documented
public @interface ExceptionHandler
Handler methods which are annotated with this annotation are allowed to have very flexible signatures. They may have arguments of the following types, in arbitrary order:
value()
.
javax.servlet.ServletRequest
/ javax.servlet.http.HttpServletRequest
or javax.portlet.PortletRequest
/ javax.portlet.ActionRequest
/
javax.portlet.RenderRequest
. Note that in the Portlet case,
an explicitly declared action/render argument is also used for mapping
specific request types onto a handler method (in case of no other
information given that differentiates between action and render requests).
javax.servlet.http.HttpSession
or javax.portlet.PortletSession
.
An argument of this type will enforce the presence of a corresponding session.
As a consequence, such an argument will never be null
.
Note that session access may not be thread-safe, in particular in a
Servlet environment: Consider switching the
"synchronizeOnSession"
flag to "true" if multiple requests are allowed to access a session concurrently.
WebRequest
or
NativeWebRequest
.
Allows for generic request parameter access as well as request/session
attribute access, without ties to the native Servlet/Portlet API.
LocaleResolver
in a Servlet environment and the portal locale in a Portlet environment).
The following return types are supported for handler methods:
ModelAndView
object (Servlet MVC or Portlet MVC).
Model
object, with the view name
implicitly determined through a RequestToViewNameTranslator
.
RequestToViewNameTranslator
.
View
object.
@ResponseBody
annotated methods (Servlet-only)
to set the response content. The return value will be converted to the
response stream using
message converters.
HttpEntity<?>
or
ResponseEntity<?>
object
(Servlet-only) to set response headers and content. The ResponseEntity body
will be converted and written to the response stream using
message converters.
void
if the method handles the response itself (by
writing the response content directly, declaring an argument of type
javax.servlet.ServletResponse
/ javax.servlet.http.HttpServletResponse
/ javax.portlet.RenderResponse
for that purpose)
or if the view name is supposed to be implicitly determined through a
RequestToViewNameTranslator
(not declaring a response argument in the handler method signature;
only applicable in a Servlet environment).
In Servlet environments, you can combine the ExceptionHandler
annotation
with @ResponseStatus
, to define the response status
for the HTTP response.
Note: In Portlet environments, ExceptionHandler
annotated methods
will only be called during the render and resource phases - just like
HandlerExceptionResolver
beans would.
Exceptions carried over from the action and event phases will be invoked during
the render phase as well, with exception handler methods having to be present
on the controller class that defines the applicable render method.
WebRequest
,
AnnotationMethodHandlerExceptionResolver
,
AnnotationMethodHandlerExceptionResolver
Modifier and Type | Optional Element and Description |
---|---|
java.lang.Class<? extends java.lang.Throwable>[] |
value
Exceptions handled by the annotation method.
|