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
ConstructorsConstructorDescriptionRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) Basic constructor with converters only.RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBodymethod 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@RequestBodyand handling@ResponseBody. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckRequired(MethodParameter parameter) voidhandleReturnValue(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 totrueto indicate the response has been handled directly.protected <T> ObjectreadWithMessageConverters(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.booleansupportsParameter(MethodParameter parameter) Whether the given method parameter is supported by this resolver.booleansupportsReturnType(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, writeWithMessageConvertersMethods 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@ResponseBodyconsider also providing aContentNegotiationManager. -
RequestResponseBodyMethodProcessor
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager) Basic constructor with converters andContentNegotiationManager. Suitable for resolving@RequestBodyand handling@ResponseBodywithoutRequest~orResponseBodyAdvice. -
RequestResponseBodyMethodProcessor
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBodymethod arguments. For handling@ResponseBodyconsider also providing aContentNegotiationManager.- Since:
- 4.2
-
RequestResponseBodyMethodProcessor
public RequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters, @Nullable ContentNegotiationManager manager, @Nullable List<Object> requestResponseBodyAdvice) Complete constructor for resolving@RequestBodyand handling@ResponseBody.
-
-
Method Details
-
supportsParameter
Description copied from interface:HandlerMethodArgumentResolverWhether the given method parameter is supported by this resolver.- Parameters:
parameter- the method parameter to check- Returns:
trueif this resolver supports the supplied parameter;falseotherwise
-
supportsReturnType
Description copied from interface:HandlerMethodReturnValueHandlerWhether the given method return type is supported by this handler.- Parameters:
returnType- the method return type to check- Returns:
trueif this handler supports the supplied return type;falseotherwise
-
resolveArgument
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 creatingWebDataBinderinstances- Returns:
- the resolved argument value, or
nullif not resolvable - Throws:
HttpMessageNotReadableException- ifRequestBody.required()istrueand 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
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException Description copied from class:AbstractMessageConverterMethodArgumentResolverCreate the method argument value of the expected parameter type by reading from the given request.- Overrides:
readWithMessageConvertersin 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:HandlerMethodReturnValueHandlerHandle the given return value by adding attributes to the model and setting a view or setting theModelAndViewContainer.setRequestHandled(boolean)flag totrueto 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:
IOExceptionHttpMediaTypeNotAcceptableExceptionHttpMessageNotWritableException
-