Class RequestResponseBodyMethodProcessor
java.lang.Object
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor
- All Implemented Interfaces:
HandlerMethodArgumentResolver
,HandlerMethodReturnValueHandler
Resolves method arguments annotated with
@RequestBody
and handles return
values from methods annotated with @ResponseBody
by reading and writing
to the body of the request or response with an HttpMessageConverter
.
An @RequestBody
method argument is also validated if it is annotated
with any
annotations that trigger validation. In case of validation failure,
MethodArgumentNotValidException
is raised and results in an HTTP 400
response status code if DefaultHandlerExceptionResolver
is configured.
- Since:
- 3.1
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
-
Field Summary
Fields inherited from class org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver
logger, messageConverters
-
Constructor Summary
ConstructorDescriptionRequestResponseBodyMethodProcessor
(List<HttpMessageConverter<?>> converters) Basic constructor with converters only.RequestResponseBodyMethodProcessor
(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBody
method arguments.RequestResponseBodyMethodProcessor
(List<HttpMessageConverter<?>> converters, ContentNegotiationManager manager) Basic constructor with converters andContentNegotiationManager
.RequestResponseBodyMethodProcessor
(List<HttpMessageConverter<?>> converters, ContentNegotiationManager manager, List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBody
and handling@ResponseBody
. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
checkRequired
(MethodParameter parameter) void
handleReturnValue
(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) Handle the given return value by adding attributes to the model and setting a view or setting theModelAndViewContainer.setRequestHandled(boolean)
flag totrue
to indicate the response has been handled directly.protected <T> Object
readWithMessageConverters
(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) Create the method argument value of the expected parameter type by reading from the given request.resolveArgument
(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) Throws MethodArgumentNotValidException if validation fails.boolean
supportsParameter
(MethodParameter parameter) Whether the given method parameter is supported by this resolver.boolean
supportsReturnType
(MethodParameter returnType) Whether the given method return type is supported by this handler.Methods inherited from class org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor
createOutputMessage, getProducibleMediaTypes, getProducibleMediaTypes, getReturnValueType, isResourceType, writeWithMessageConverters, writeWithMessageConverters
Methods inherited from class org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver
adaptArgumentIfNecessary, createInputMessage, getSupportedMediaTypes, isBindExceptionRequired, readWithMessageConverters, validateIfApplicable
-
Constructor Details
-
RequestResponseBodyMethodProcessor
Basic constructor with converters only. Suitable for resolving@RequestBody
. For handling@ResponseBody
consider also providing aContentNegotiationManager
. -
RequestResponseBodyMethodProcessor
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager) Basic constructor with converters andContentNegotiationManager
. Suitable for resolving@RequestBody
and handling@ResponseBody
withoutRequest~
orResponseBodyAdvice
. -
RequestResponseBodyMethodProcessor
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBody
method arguments. For handling@ResponseBody
consider also providing aContentNegotiationManager
.- Since:
- 4.2
-
RequestResponseBodyMethodProcessor
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager, @Nullable List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBody
and handling@ResponseBody
.
-
-
Method Details
-
supportsParameter
Description copied from interface:HandlerMethodArgumentResolver
Whether the given method parameter is supported by this resolver.- Parameters:
parameter
- the method parameter to check- Returns:
true
if this resolver supports the supplied parameter;false
otherwise
-
supportsReturnType
Description copied from interface:HandlerMethodReturnValueHandler
Whether the given method return type is supported by this handler.- Parameters:
returnType
- the method return type to check- Returns:
true
if this handler supports the supplied return type;false
otherwise
-
resolveArgument
@Nullable public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception Throws MethodArgumentNotValidException if validation fails.- Parameters:
parameter
- the method parameter to resolve. This parameter must have previously been passed toHandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter)
which must have returnedtrue
.mavContainer
- the ModelAndViewContainer for the current requestwebRequest
- the current requestbinderFactory
- a factory for creatingWebDataBinder
instances- Returns:
- the resolved argument value, or
null
if not resolvable - Throws:
HttpMessageNotReadableException
- ifRequestBody.required()
istrue
and there is no body content or if there is no suitable converter to read the content with.Exception
- in case of errors with the preparation of argument values
-
readWithMessageConverters
@Nullable protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException Description copied from class:AbstractMessageConverterMethodArgumentResolver
Create the method argument value of the expected parameter type by reading from the given request.- Overrides:
readWithMessageConverters
in classAbstractMessageConverterMethodArgumentResolver
- Type Parameters:
T
- the expected type of the argument value to be created- Parameters:
webRequest
- the current requestparameter
- the method parameter descriptor (may benull
)paramType
- the type of the argument value to be created- Returns:
- the created method argument value
- Throws:
IOException
- if the reading from the request failsHttpMediaTypeNotSupportedException
- if no suitable message converter is foundHttpMessageNotReadableException
-
checkRequired
-
handleReturnValue
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException Description copied from interface:HandlerMethodReturnValueHandler
Handle the given return value by adding attributes to the model and setting a view or setting theModelAndViewContainer.setRequestHandled(boolean)
flag totrue
to indicate the response has been handled directly.- Parameters:
returnValue
- the value returned from the handler methodreturnType
- the type of the return value. This type must have previously been passed toHandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)
which must have returnedtrue
.mavContainer
- the ModelAndViewContainer for the current requestwebRequest
- the current request- Throws:
IOException
HttpMediaTypeNotAcceptableException
HttpMessageNotWritableException
-