public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware, InitializingBean
AbstractHandlerMethodAdapter
that supports HandlerMethod
s
with their method argument and return type signature, as defined via
@RequestMapping
.
Support for custom argument and return value types can be added via
setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)
and setCustomReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)
.
Or alternatively, to re-configure all argument and return value types,
use setArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)
and setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)
.
HandlerMethodArgumentResolver
,
HandlerMethodReturnValueHandler
HEADER_CACHE_CONTROL, METHOD_GET, METHOD_HEAD, METHOD_POST
logger
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
RequestMappingHandlerAdapter() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected InitBinderDataBinderFactory |
createDataBinderFactory(java.util.List<InvocableHandlerMethod> binderMethods)
Template method to create a new InitBinderDataBinderFactory instance.
|
private InvocableHandlerMethod |
createInitBinderMethod(java.lang.Object bean,
java.lang.reflect.Method method) |
protected ServletInvocableHandlerMethod |
createInvocableHandlerMethod(HandlerMethod handlerMethod)
Create a
ServletInvocableHandlerMethod from the given HandlerMethod definition. |
private InvocableHandlerMethod |
createModelAttributeMethod(WebDataBinderFactory factory,
java.lang.Object bean,
java.lang.reflect.Method method) |
java.util.List<HandlerMethodArgumentResolver> |
getArgumentResolvers()
Return the configured argument resolvers, or possibly
null if
not initialized yet via afterPropertiesSet() . |
protected ConfigurableBeanFactory |
getBeanFactory()
Return the owning factory of this bean instance, or
null if none. |
java.util.List<HandlerMethodArgumentResolver> |
getCustomArgumentResolvers()
Return the custom argument resolvers, or
null . |
java.util.List<HandlerMethodReturnValueHandler> |
getCustomReturnValueHandlers()
Return the custom return value handlers, or
null . |
private WebDataBinderFactory |
getDataBinderFactory(HandlerMethod handlerMethod) |
private java.util.List<HandlerMethodArgumentResolver> |
getDefaultArgumentResolvers()
Return the list of argument resolvers to use including built-in resolvers
and custom resolvers provided via
setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>) . |
private java.util.List<HandlerMethodArgumentResolver> |
getDefaultInitBinderArgumentResolvers()
Return the list of argument resolvers to use for
@InitBinder
methods including built-in and custom resolvers. |
private java.util.List<HandlerMethodReturnValueHandler> |
getDefaultReturnValueHandlers()
Return the list of return value handlers to use including built-in and
custom handlers provided via
setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>) . |
java.util.List<HandlerMethodArgumentResolver> |
getInitBinderArgumentResolvers()
Return the argument resolvers for
@InitBinder methods, or possibly
null if not initialized yet via afterPropertiesSet() . |
protected long |
getLastModifiedInternal(HttpServletRequest request,
HandlerMethod handlerMethod)
This implementation always returns -1.
|
java.util.List<HttpMessageConverter<?>> |
getMessageConverters()
Return the configured message body converters.
|
private ModelAndView |
getModelAndView(ModelAndViewContainer mavContainer,
ModelFactory modelFactory,
NativeWebRequest webRequest) |
java.util.List<ModelAndViewResolver> |
getModelAndViewResolvers()
Return the configured
ModelAndViewResolver s, or null . |
private ModelFactory |
getModelFactory(HandlerMethod handlerMethod,
WebDataBinderFactory binderFactory) |
java.util.List<HandlerMethodReturnValueHandler> |
getReturnValueHandlers()
Return the configured handlers, or possibly
null if not
initialized yet via afterPropertiesSet() . |
private SessionAttributesHandler |
getSessionAttributesHandler(HandlerMethod handlerMethod)
Return the
SessionAttributesHandler instance for the given handler type
(never null ). |
WebBindingInitializer |
getWebBindingInitializer()
Return the configured WebBindingInitializer, or
null if none. |
protected ModelAndView |
handleInternal(HttpServletRequest request,
HttpServletResponse response,
HandlerMethod handlerMethod)
Use the given handler method to handle the request.
|
private void |
initControllerAdviceCache() |
protected ModelAndView |
invokeHandlerMethod(HttpServletRequest request,
HttpServletResponse response,
HandlerMethod handlerMethod)
Invoke the
RequestMapping handler method preparing a ModelAndView
if view resolution is required. |
void |
setArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
Configure the complete list of supported argument types thus overriding
the resolvers that would otherwise be configured by default.
|
void |
setAsyncRequestTimeout(long timeout)
Specify the amount of time, in milliseconds, before concurrent handling
should time out.
|
void |
setBeanFactory(BeanFactory beanFactory)
A
ConfigurableBeanFactory is expected for resolving expressions
in method argument default values. |
void |
setCacheSecondsForSessionAttributeHandlers(int cacheSecondsForSessionAttributeHandlers)
Cache content produced by
@SessionAttributes annotated handlers
for the given number of seconds. |
void |
setCallableInterceptors(java.util.List<CallableProcessingInterceptor> interceptors)
Configure
CallableProcessingInterceptor 's to register on async requests. |
void |
setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Set the
ContentNegotiationManager to use to determine requested media types. |
void |
setCustomArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
Provide resolvers for custom argument types.
|
void |
setCustomReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
Provide handlers for custom return value types.
|
void |
setDeferredResultInterceptors(java.util.List<DeferredResultProcessingInterceptor> interceptors)
Configure
DeferredResultProcessingInterceptor 's to register on async requests. |
void |
setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)
By default the content of the "default" model is used both during
rendering and redirect scenarios.
|
void |
setInitBinderArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
Configure the supported argument types in
@InitBinder methods. |
void |
setMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
Provide the converters to use in argument resolvers and return value
handlers that support reading and/or writing to the body of the
request and response.
|
void |
setModelAndViewResolvers(java.util.List<ModelAndViewResolver> modelAndViewResolvers)
Provide custom
ModelAndViewResolver s. |
void |
setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)
Set the ParameterNameDiscoverer to use for resolving method parameter names if needed
(e.g.
|
void |
setRequestBodyAdvice(java.util.List<RequestBodyAdvice> requestBodyAdvice)
Add one or more
RequestBodyAdvice instances to intercept the
request before it is read and converted for @RequestBody and
HttpEntity method arguments. |
void |
setResponseBodyAdvice(java.util.List<ResponseBodyAdvice<?>> responseBodyAdvice)
Add one or more
ResponseBodyAdvice instances to intercept the
response before @ResponseBody or ResponseEntity return
values are written to the response body. |
void |
setReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
Configure the complete list of supported return value types thus
overriding handlers that would otherwise be configured by default.
|
void |
setSessionAttributeStore(SessionAttributeStore sessionAttributeStore)
Specify the strategy to store session attributes with.
|
void |
setSynchronizeOnSession(boolean synchronizeOnSession)
Set if controller execution should be synchronized on the session,
to serialize parallel invocations from the same client.
|
void |
setTaskExecutor(AsyncTaskExecutor taskExecutor)
Set the default
AsyncTaskExecutor to use when a controller method
return a Callable . |
void |
setWebBindingInitializer(WebBindingInitializer webBindingInitializer)
Provide a WebBindingInitializer with "global" initialization to apply
to every DataBinder instance.
|
protected boolean |
supportsInternal(HandlerMethod handlerMethod)
Always return
true since any method argument and return value
type will be processed in some way. |
getLastModified, getOrder, handle, setOrder, supports
applyCacheControl, applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, checkRequest, getAllowHeader, getCacheControl, getCacheSeconds, getSupportedMethods, getVaryByRequestHeaders, isAlwaysMustRevalidate, isRequireSession, isUseCacheControlHeader, isUseCacheControlNoStore, isUseExpiresHeader, prepareResponse, preventCaching, setAlwaysMustRevalidate, setCacheControl, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseCacheControlNoStore, setUseExpiresHeader, setVaryByRequestHeaders
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
private java.util.List<HandlerMethodArgumentResolver> customArgumentResolvers
private HandlerMethodArgumentResolverComposite argumentResolvers
private HandlerMethodArgumentResolverComposite initBinderArgumentResolvers
private java.util.List<HandlerMethodReturnValueHandler> customReturnValueHandlers
private HandlerMethodReturnValueHandlerComposite returnValueHandlers
private java.util.List<ModelAndViewResolver> modelAndViewResolvers
private ContentNegotiationManager contentNegotiationManager
private java.util.List<HttpMessageConverter<?>> messageConverters
private java.util.List<java.lang.Object> requestResponseBodyAdvice
private WebBindingInitializer webBindingInitializer
private AsyncTaskExecutor taskExecutor
private java.lang.Long asyncRequestTimeout
private CallableProcessingInterceptor[] callableInterceptors
private DeferredResultProcessingInterceptor[] deferredResultInterceptors
private boolean ignoreDefaultModelOnRedirect
private int cacheSecondsForSessionAttributeHandlers
private boolean synchronizeOnSession
private SessionAttributeStore sessionAttributeStore
private ParameterNameDiscoverer parameterNameDiscoverer
private ConfigurableBeanFactory beanFactory
private final java.util.Map<java.lang.Class<?>,SessionAttributesHandler> sessionAttributesHandlerCache
private final java.util.Map<java.lang.Class<?>,java.util.Set<java.lang.reflect.Method>> initBinderCache
private final java.util.Map<ControllerAdviceBean,java.util.Set<java.lang.reflect.Method>> initBinderAdviceCache
private final java.util.Map<java.lang.Class<?>,java.util.Set<java.lang.reflect.Method>> modelAttributeCache
private final java.util.Map<ControllerAdviceBean,java.util.Set<java.lang.reflect.Method>> modelAttributeAdviceCache
public static final ReflectionUtils.MethodFilter INIT_BINDER_METHODS
@InitBinder
methods.public static final ReflectionUtils.MethodFilter MODEL_ATTRIBUTE_METHODS
@ModelAttribute
methods.public void setCustomArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
setArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)
instead.public java.util.List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()
null
.public void setArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
public java.util.List<HandlerMethodArgumentResolver> getArgumentResolvers()
null
if
not initialized yet via afterPropertiesSet()
.public void setInitBinderArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> argumentResolvers)
@InitBinder
methods.public java.util.List<HandlerMethodArgumentResolver> getInitBinderArgumentResolvers()
@InitBinder
methods, or possibly
null
if not initialized yet via afterPropertiesSet()
.public void setCustomReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)
.public java.util.List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()
null
.public void setReturnValueHandlers(java.util.List<HandlerMethodReturnValueHandler> returnValueHandlers)
public java.util.List<HandlerMethodReturnValueHandler> getReturnValueHandlers()
null
if not
initialized yet via afterPropertiesSet()
.public void setModelAndViewResolvers(java.util.List<ModelAndViewResolver> modelAndViewResolvers)
ModelAndViewResolver
s.
Note: This method is available for backwards
compatibility only. However, it is recommended to re-write a
ModelAndViewResolver
as HandlerMethodReturnValueHandler
.
An adapter between the two interfaces is not possible since the
HandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)
method
cannot be implemented. Hence ModelAndViewResolver
s are limited
to always being invoked at the end after all other return value
handlers have been given a chance.
A HandlerMethodReturnValueHandler
provides better access to
the return type and controller method information and can be ordered
freely relative to other return value handlers.
public java.util.List<ModelAndViewResolver> getModelAndViewResolvers()
ModelAndViewResolver
s, or null
.public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
ContentNegotiationManager
to use to determine requested media types.
If not set, the default constructor is used.public void setMessageConverters(java.util.List<HttpMessageConverter<?>> messageConverters)
public java.util.List<HttpMessageConverter<?>> getMessageConverters()
public void setRequestBodyAdvice(java.util.List<RequestBodyAdvice> requestBodyAdvice)
RequestBodyAdvice
instances to intercept the
request before it is read and converted for @RequestBody
and
HttpEntity
method arguments.public void setResponseBodyAdvice(java.util.List<ResponseBodyAdvice<?>> responseBodyAdvice)
ResponseBodyAdvice
instances to intercept the
response before @ResponseBody
or ResponseEntity
return
values are written to the response body.public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer)
public WebBindingInitializer getWebBindingInitializer()
null
if none.public void setTaskExecutor(AsyncTaskExecutor taskExecutor)
AsyncTaskExecutor
to use when a controller method
return a Callable
. Controller methods can override this default on
a per-request basis by returning an WebAsyncTask
.
By default a SimpleAsyncTaskExecutor
instance is used.
It's recommended to change that default in production as the simple executor
does not re-use threads.
public void setAsyncRequestTimeout(long timeout)
If this value is not set, the default timeout of the underlying implementation is used, e.g. 10 seconds on Tomcat with Servlet 3.
timeout
- the timeout value in millisecondspublic void setCallableInterceptors(java.util.List<CallableProcessingInterceptor> interceptors)
CallableProcessingInterceptor
's to register on async requests.interceptors
- the interceptors to registerpublic void setDeferredResultInterceptors(java.util.List<DeferredResultProcessingInterceptor> interceptors)
DeferredResultProcessingInterceptor
's to register on async requests.interceptors
- the interceptors to registerpublic void setIgnoreDefaultModelOnRedirect(boolean ignoreDefaultModelOnRedirect)
RedirectAttributes
argument and use it to provide
attributes for a redirect.
Setting this flag to true
guarantees the "default" model is
never used in a redirect scenario even if a RedirectAttributes argument
is not declared. Setting it to false
means the "default" model
may be used in a redirect if the controller method doesn't declare a
RedirectAttributes argument.
The default setting is false
but new applications should
consider setting it to true
.
RedirectAttributes
public void setSessionAttributeStore(SessionAttributeStore sessionAttributeStore)
DefaultSessionAttributeStore
,
storing session attributes in the HttpSession with the same attribute
name as in the model.public void setCacheSecondsForSessionAttributeHandlers(int cacheSecondsForSessionAttributeHandlers)
@SessionAttributes
annotated handlers
for the given number of seconds.
Possible values are:
In contrast to the "cacheSeconds" property which will apply to all general
handlers (but not to @SessionAttributes
annotated handlers),
this setting will apply to @SessionAttributes
handlers only.
public void setSynchronizeOnSession(boolean synchronizeOnSession)
More specifically, the execution of the handleRequestInternal
method will get synchronized if this flag is "true". The best available
session mutex will be used for the synchronization; ideally, this will
be a mutex exposed by HttpSessionMutexListener.
The session mutex is guaranteed to be the same object during
the entire lifetime of the session, available under the key defined
by the SESSION_MUTEX_ATTRIBUTE
constant. It serves as a
safe reference to synchronize on for locking on the current session.
In many cases, the HttpSession reference itself is a safe mutex as well, since it will always be the same object reference for the same active logical session. However, this is not guaranteed across different servlet containers; the only 100% safe way is a session mutex.
HttpSessionMutexListener
,
org.springframework.web.util.WebUtils#getSessionMutex(javax.servlet.http.HttpSession)
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)
Default is a DefaultParameterNameDiscoverer
.
public void setBeanFactory(BeanFactory beanFactory)
ConfigurableBeanFactory
is expected for resolving expressions
in method argument default values.setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
protected ConfigurableBeanFactory getBeanFactory()
null
if none.public void afterPropertiesSet()
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
private void initControllerAdviceCache()
private java.util.List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers()
setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)
.private java.util.List<HandlerMethodArgumentResolver> getDefaultInitBinderArgumentResolvers()
@InitBinder
methods including built-in and custom resolvers.private java.util.List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers()
setReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>)
.protected boolean supportsInternal(HandlerMethod handlerMethod)
true
since any method argument and return value
type will be processed in some way. A method argument not recognized
by any HandlerMethodArgumentResolver is interpreted as a request parameter
if it is a simple type, or as a model attribute otherwise. A return value
not recognized by any HandlerMethodReturnValueHandler will be interpreted
as a model attribute.supportsInternal
in class AbstractHandlerMethodAdapter
handlerMethod
- the handler method to checkprotected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws java.lang.Exception
AbstractHandlerMethodAdapter
handleInternal
in class AbstractHandlerMethodAdapter
request
- current HTTP requestresponse
- current HTTP responsehandlerMethod
- handler method to use. This object must have previously been passed to the
AbstractHandlerMethodAdapter.supportsInternal(HandlerMethod)
this interface, which must have returned true
.null
if the request has been handled directlyjava.lang.Exception
- in case of errorsprotected long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod)
@RequestMapping
method can
calculate the lastModified value, call WebRequest.checkNotModified(long)
,
and return null
if the result of that call is true
.getLastModifiedInternal
in class AbstractHandlerMethodAdapter
request
- current HTTP requesthandlerMethod
- handler method to useprivate SessionAttributesHandler getSessionAttributesHandler(HandlerMethod handlerMethod)
SessionAttributesHandler
instance for the given handler type
(never null
).protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws java.lang.Exception
RequestMapping
handler method preparing a ModelAndView
if view resolution is required.java.lang.Exception
createInvocableHandlerMethod(HandlerMethod)
protected ServletInvocableHandlerMethod createInvocableHandlerMethod(HandlerMethod handlerMethod)
ServletInvocableHandlerMethod
from the given HandlerMethod
definition.handlerMethod
- the HandlerMethod
definitionServletInvocableHandlerMethod
(or custom subclass thereof)private ModelFactory getModelFactory(HandlerMethod handlerMethod, WebDataBinderFactory binderFactory)
private InvocableHandlerMethod createModelAttributeMethod(WebDataBinderFactory factory, java.lang.Object bean, java.lang.reflect.Method method)
private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws java.lang.Exception
java.lang.Exception
private InvocableHandlerMethod createInitBinderMethod(java.lang.Object bean, java.lang.reflect.Method method)
protected InitBinderDataBinderFactory createDataBinderFactory(java.util.List<InvocableHandlerMethod> binderMethods) throws java.lang.Exception
The default implementation creates a ServletRequestDataBinderFactory. This can be overridden for custom ServletRequestDataBinder subclasses.
binderMethods
- @InitBinder
methodsjava.lang.Exception
- in case of invalid state or argumentsprivate ModelAndView getModelAndView(ModelAndViewContainer mavContainer, ModelFactory modelFactory, NativeWebRequest webRequest) throws java.lang.Exception
java.lang.Exception