001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.bind.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025/** 026 * Annotation for handling exceptions in specific handler classes and/or 027 * handler methods. 028 * 029 * <p>Handler methods which are annotated with this annotation are allowed to 030 * have very flexible signatures. They may have parameters of the following 031 * types, in arbitrary order: 032 * <ul> 033 * <li>An exception argument: declared as a general Exception or as a more 034 * specific exception. This also serves as a mapping hint if the annotation 035 * itself does not narrow the exception types through its {@link #value()}. 036 * <li>Request and/or response objects (typically from the Servlet API). 037 * You may choose any specific request/response type, e.g. 038 * {@link javax.servlet.ServletRequest} / {@link javax.servlet.http.HttpServletRequest}. 039 * <li>Session object: typically {@link javax.servlet.http.HttpSession}. 040 * An argument of this type will enforce the presence of a corresponding session. 041 * As a consequence, such an argument will never be {@code null}. 042 * <i>Note that session access may not be thread-safe, in particular in a 043 * Servlet environment: Consider switching the 044 * {@link org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#setSynchronizeOnSession 045 * "synchronizeOnSession"} flag to "true" if multiple requests are allowed to 046 * access a session concurrently.</i> 047 * <li>{@link org.springframework.web.context.request.WebRequest} or 048 * {@link org.springframework.web.context.request.NativeWebRequest}. 049 * Allows for generic request parameter access as well as request/session 050 * attribute access, without ties to the native Servlet API. 051 * <li>{@link java.util.Locale} for the current request locale 052 * (determined by the most specific locale resolver available, 053 * i.e. the configured {@link org.springframework.web.servlet.LocaleResolver} 054 * in a Servlet environment). 055 * <li>{@link java.io.InputStream} / {@link java.io.Reader} for access 056 * to the request's content. This will be the raw InputStream/Reader as 057 * exposed by the Servlet API. 058 * <li>{@link java.io.OutputStream} / {@link java.io.Writer} for generating 059 * the response's content. This will be the raw OutputStream/Writer as 060 * exposed by the Servlet API. 061 * <li>{@link org.springframework.ui.Model} as an alternative to returning 062 * a model map from the handler method. Note that the provided model is not 063 * pre-populated with regular model attributes and therefore always empty, 064 * as a convenience for preparing the model for an exception-specific view. 065 * </ul> 066 * 067 * <p>The following return types are supported for handler methods: 068 * <ul> 069 * <li>A {@code ModelAndView} object (from Servlet MVC). 070 * <li>A {@link org.springframework.ui.Model} object, with the view name implicitly 071 * determined through a {@link org.springframework.web.servlet.RequestToViewNameTranslator}. 072 * <li>A {@link java.util.Map} object for exposing a model, 073 * with the view name implicitly determined through a 074 * {@link org.springframework.web.servlet.RequestToViewNameTranslator}. 075 * <li>A {@link org.springframework.web.servlet.View} object. 076 * <li>A {@link String} value which is interpreted as view name. 077 * <li>{@link ResponseBody @ResponseBody} annotated methods (Servlet-only) 078 * to set the response content. The return value will be converted to the 079 * response stream using 080 * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. 081 * <li>An {@link org.springframework.http.HttpEntity HttpEntity<?>} or 082 * {@link org.springframework.http.ResponseEntity ResponseEntity<?>} object 083 * (Servlet-only) to set response headers and content. The ResponseEntity body 084 * will be converted and written to the response stream using 085 * {@linkplain org.springframework.http.converter.HttpMessageConverter message converters}. 086 * <li>{@code void} if the method handles the response itself (by 087 * writing the response content directly, declaring an argument of type 088 * {@link javax.servlet.ServletResponse} / {@link javax.servlet.http.HttpServletResponse} 089 * for that purpose) or if the view name is supposed to be implicitly determined 090 * through a {@link org.springframework.web.servlet.RequestToViewNameTranslator} 091 * (not declaring a response argument in the handler method signature). 092 * </ul> 093 * 094 * <p>You may combine the {@code ExceptionHandler} annotation with 095 * {@link ResponseStatus @ResponseStatus} for a specific HTTP error status. 096 * 097 * @author Arjen Poutsma 098 * @author Juergen Hoeller 099 * @since 3.0 100 * @see org.springframework.web.context.request.WebRequest 101 */ 102@Target(ElementType.METHOD) 103@Retention(RetentionPolicy.RUNTIME) 104@Documented 105public @interface ExceptionHandler { 106 107 /** 108 * Exceptions handled by the annotated method. If empty, will default to any 109 * exceptions listed in the method argument list. 110 */ 111 Class<? extends Throwable>[] value() default {}; 112 113}