public class RequestResponseBodyMethodProcessor extends AbstractMessageConverterMethodProcessor
@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.
logger, messageConverters
Constructor and Description |
---|
RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters)
Basic constructor with converters only.
|
RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters,
ContentNegotiationManager manager)
Basic constructor with converters and
ContentNegotiationManager . |
RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters,
ContentNegotiationManager manager,
List<Object> requestResponseBodyAdvice)
Complete constructor for resolving
@RequestBody and handling
@ResponseBody . |
RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters,
List<Object> requestResponseBodyAdvice)
Complete constructor for resolving
@RequestBody method arguments. |
Modifier and Type | Method and Description |
---|---|
protected 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 the
ModelAndViewContainer.setRequestHandled(boolean) flag to true
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.
|
Object |
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.
|
createOutputMessage, getProducibleMediaTypes, getProducibleMediaTypes, getReturnValueType, isResourceType, writeWithMessageConverters, writeWithMessageConverters
adaptArgumentIfNecessary, createInputMessage, getSupportedMediaTypes, isBindExceptionRequired, readWithMessageConverters, validateIfApplicable
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters)
@RequestBody
. For handling @ResponseBody
consider also
providing a ContentNegotiationManager
.public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager)
ContentNegotiationManager
.
Suitable for resolving @RequestBody
and handling
@ResponseBody
without Request~
or
ResponseBodyAdvice
.public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice)
@RequestBody
method arguments.
For handling @ResponseBody
consider also providing a
ContentNegotiationManager
.public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager, @Nullable List<Object> requestResponseBodyAdvice)
@RequestBody
and handling
@ResponseBody
.public boolean supportsParameter(MethodParameter parameter)
HandlerMethodArgumentResolver
parameter
- the method parameter to checktrue
if this resolver supports the supplied parameter;
false
otherwisepublic boolean supportsReturnType(MethodParameter returnType)
HandlerMethodReturnValueHandler
returnType
- the method return type to checktrue
if this handler supports the supplied return type;
false
otherwisepublic Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception
parameter
- the method parameter to resolve. This parameter must
have previously been passed to HandlerMethodArgumentResolver.supportsParameter(org.springframework.core.MethodParameter)
which must
have returned true
.mavContainer
- the ModelAndViewContainer for the current requestwebRequest
- the current requestbinderFactory
- a factory for creating WebDataBinder
instancesnull
if not resolvableHttpMessageNotReadableException
- if RequestBody.required()
is true
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 valuesprotected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException
AbstractMessageConverterMethodArgumentResolver
readWithMessageConverters
in class AbstractMessageConverterMethodArgumentResolver
T
- the expected type of the argument value to be createdwebRequest
- the current requestparameter
- the method parameter descriptor (may be null
)paramType
- the type of the argument value to be createdIOException
- if the reading from the request failsHttpMediaTypeNotSupportedException
- if no suitable message converter is foundHttpMessageNotReadableException
protected boolean checkRequired(MethodParameter parameter)
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException
HandlerMethodReturnValueHandler
ModelAndViewContainer.setRequestHandled(boolean)
flag to true
to indicate the response has been handled directly.returnValue
- the value returned from the handler methodreturnType
- the type of the return value. This type must have
previously been passed to HandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)
which must
have returned true
.mavContainer
- the ModelAndViewContainer for the current requestwebRequest
- the current requestIOException
HttpMediaTypeNotAcceptableException
HttpMessageNotWritableException