Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.mvc.method.annotation

public abstract Class AbstractMessageConverterMethodArgumentResolver

extends Object
implements HandlerMethodArgumentResolver
Class Inheritance
All Implemented Interfaces
org.springframework.web.method.support.HandlerMethodArgumentResolver
Known Direct Subclasses
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor, org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver
Imports
java.io.IOException, .InputStream, .PushbackInputStream, java.lang.annotation.Annotation, java.lang.reflect.Type, java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .List, .Optional, .Set, jakarta.servlet.http.HttpServletRequest, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.MethodParameter, .ResolvableType, org.springframework.core.log.LogFormatUtils, org.springframework.http.HttpHeaders, .HttpInputMessage, .HttpMethod, .HttpOutputMessage, .HttpRequest, .InvalidMediaTypeException, .MediaType, org.springframework.http.converter.GenericHttpMessageConverter, .HttpMessageConverter, .HttpMessageNotReadableException, org.springframework.http.server.ServletServerHttpRequest, org.springframework.lang.Nullable, org.springframework.util.Assert, .MimeTypeUtils, org.springframework.validation.Errors, org.springframework.validation.annotation.ValidationAnnotationUtils, org.springframework.web.HttpMediaTypeNotSupportedException, org.springframework.web.bind.WebDataBinder, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.method.support.HandlerMethodArgumentResolver

A base class for resolving method argument values by reading from the body of a request with HttpMessageConverters.
Authors
Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
AbstractMessageConverterMethodArgumentResolver.NoContentTypeHttpMessageConverter

Placeholder HttpMessageConverter type to pass to RequestBodyAdvice if there is no content-type and no content.

Field Summary

Modifier and TypeField and Description
private final RequestResponseBodyAdviceChain
protected final Log
protected final List<HttpMessageConverter<?>>
private static final Object
private static final Set<HttpMethod>

Constructor Summary

AccessConstructor and Description
public
AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters)

Basic constructor with converters only.

public
AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice)

Constructor with converters and Request~ and ResponseBodyAdvice.

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

the adapted argument, or the original resolved argument as-is
adaptArgumentIfNecessary
(Object
the resolved argument
arg
,
MethodParameter
the method parameter descriptor
parameter
)

Adapt the given argument against the method parameter, if necessary.

pack-priv void
closeStreamIfNecessary(InputStream body)

Allow for closing the body stream if necessary, e.g. for part streams in a multipart request.

protected ServletServerHttpRequest

Returns:

the input message
createInputMessage
(NativeWebRequest
the web request to create an input message from
webRequest
)

Create a new HttpInputMessage from the given NativeWebRequest.

pack-priv RequestResponseBodyAdviceChain
getAdvice()

Return the configured RequestBodyAdvice and RequestBodyAdvice where each instance may be wrapped as a ControllerAdviceBean.

protected List<MediaType>
getSupportedMediaTypes(Class<?> clazz)

Return the media types supported by all provided message converters sorted by specificity via MimeTypeUtils#sortBySpecificity(List).

protected boolean

Returns:

true if the next method argument is not of type Errors
isBindExceptionRequired
(WebDataBinder
the data binder used to perform data binding
binder
,
MethodParameter
the method parameter descriptor
parameter
)

Whether to raise a fatal bind exception on validation errors.

protected <
the expected type of the argument value to be created
T
>
Object

Returns:

the created method argument value
readWithMessageConverters
(NativeWebRequest
the current request
webRequest
,
MethodParameter
the method parameter descriptor (may be null)
parameter
,
Type
the type of the argument value to be created
paramType
)

Create the method argument value of the expected parameter type by reading from the given request.

protected <
the expected type of the argument value to be created
T
>
Object

Returns:

the created method argument value
readWithMessageConverters
(HttpInputMessage
the HTTP input message representing the current request
inputMessage
,
MethodParameter
the method parameter descriptor
parameter
,
Type
the target type, not necessarily the same as the method parameter type, e.g. for HttpEntity<String>.
targetType
)

Create the method argument value of the expected parameter type by reading from the given HttpInputMessage.

protected void
validateIfApplicable(WebDataBinder
the DataBinder to be used
binder
,
MethodParameter
the method parameter descriptor
parameter
)

Validate the binding target if applicable.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

adviceback to summary
private final RequestResponseBodyAdviceChain advice
loggerback to summary
protected final Log logger
messageConvertersback to summary
protected final List<HttpMessageConverter<?>> messageConverters
NO_VALUEback to summary
private static final Object NO_VALUE
SUPPORTED_METHODSback to summary
private static final Set<HttpMethod> SUPPORTED_METHODS

Constructor Detail

AbstractMessageConverterMethodArgumentResolverback to summary
public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters)

Basic constructor with converters only.

AbstractMessageConverterMethodArgumentResolverback to summary
public AbstractMessageConverterMethodArgumentResolver(List<HttpMessageConverter<?>> converters, List<Object> requestResponseBodyAdvice)

Constructor with converters and Request~ and ResponseBodyAdvice.

Since
4.2

Method Detail

adaptArgumentIfNecessaryback to summary
protected Object adaptArgumentIfNecessary(Object arg, MethodParameter parameter)

Adapt the given argument against the method parameter, if necessary.

Parameters
arg:Object

the resolved argument

parameter:MethodParameter

the method parameter descriptor

Returns:Object

the adapted argument, or the original resolved argument as-is

Annotations
@Nullable
Since
4.3.5
closeStreamIfNecessaryback to summary
pack-priv void closeStreamIfNecessary(InputStream body)

Allow for closing the body stream if necessary, e.g. for part streams in a multipart request.

createInputMessageback to summary
protected ServletServerHttpRequest createInputMessage(NativeWebRequest webRequest)

Create a new HttpInputMessage from the given NativeWebRequest.

Parameters
webRequest:NativeWebRequest

the web request to create an input message from

Returns:ServletServerHttpRequest

the input message

getAdviceback to summary
pack-priv RequestResponseBodyAdviceChain getAdvice()

Return the configured RequestBodyAdvice and RequestBodyAdvice where each instance may be wrapped as a ControllerAdviceBean.

getSupportedMediaTypesback to summary
protected List<MediaType> getSupportedMediaTypes(Class<?> clazz)

Return the media types supported by all provided message converters sorted by specificity via MimeTypeUtils#sortBySpecificity(List).

Since
5.3.4
isBindExceptionRequiredback to summary
protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter)

Whether to raise a fatal bind exception on validation errors.

Parameters
binder:WebDataBinder

the data binder used to perform data binding

parameter:MethodParameter

the method parameter descriptor

Returns:boolean

true if the next method argument is not of type Errors

Since
4.1.5
readWithMessageConvertersback to summary
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException

Create the method argument value of the expected parameter type by reading from the given request.

Parameters
<T>
the expected type of the argument value to be created
webRequest:NativeWebRequest

the current request

parameter:MethodParameter

the method parameter descriptor (may be null)

paramType:Type

the type of the argument value to be created

Returns:Object

the created method argument value

Annotations
@Nullable
Exceptions
IOException:
if the reading from the request fails
HttpMediaTypeNotSupportedException:
if no suitable message converter is found
readWithMessageConvertersback to summary
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException

Create the method argument value of the expected parameter type by reading from the given HttpInputMessage.

Parameters
<T>
the expected type of the argument value to be created
inputMessage:HttpInputMessage

the HTTP input message representing the current request

parameter:MethodParameter

the method parameter descriptor

targetType:Type

the target type, not necessarily the same as the method parameter type, e.g. for HttpEntity<String>.

Returns:Object

the created method argument value

Annotations
@Nullable
@SuppressWarnings:rawtypes, unchecked
Exceptions
IOException:
if the reading from the request fails
HttpMediaTypeNotSupportedException:
if no suitable message converter is found
validateIfApplicableback to summary
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter)

Validate the binding target if applicable.

The default implementation checks for @jakarta.validation.Valid, Spring's org.springframework.validation.annotation.Validated, and custom annotations whose name starts with "Valid".

Parameters
binder:WebDataBinder

the DataBinder to be used

parameter:MethodParameter

the method parameter descriptor

Since
4.1.5
See Also
isBindExceptionRequired
org.springframework.web.servlet.mvc.method.annotation back to summary

private Class AbstractMessageConverterMethodArgumentResolver.EmptyBodyCheckingHttpInputMessage

extends Object
implements HttpInputMessage
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.EmptyBodyCheckingHttpInputMessage
All Implemented Interfaces
org.springframework.http.HttpInputMessage, org.springframework.http.HttpMessage

Field Summary

Modifier and TypeField and Description
private final InputStream
private final HttpHeaders

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public InputStream
getBody()

Implements org.springframework.http.HttpInputMessage.getBody.

Return the body of the message as an input stream.

public HttpHeaders
getHeaders()

Implements org.springframework.http.HttpMessage.getHeaders.

Return the headers of this message.

public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bodyback to summary
private final InputStream body
Annotations
@Nullable
headersback to summary
private final HttpHeaders headers

Constructor Detail

EmptyBodyCheckingHttpInputMessageback to summary
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException

Method Detail

getBodyback to summary
public InputStream getBody()

Implements org.springframework.http.HttpInputMessage.getBody.

Doc from org.springframework.http.HttpInputMessage.getBody.

Return the body of the message as an input stream.

Returns:InputStream

the input stream body (never null)

Annotations
@Override
getHeadersback to summary
public HttpHeaders getHeaders()

Implements org.springframework.http.HttpMessage.getHeaders.

Doc from org.springframework.http.HttpMessage.getHeaders.

Return the headers of this message.

Returns:HttpHeaders

a corresponding HttpHeaders object (never null)

Annotations
@Override
hasBodyback to summary
public boolean hasBody()
org.springframework.web.servlet.mvc.method.annotation back to summary

private Class AbstractMessageConverterMethodArgumentResolver.NoContentTypeHttpMessageConverter

extends Object
implements HttpMessageConverter<String>
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.NoContentTypeHttpMessageConverter
All Implemented Interfaces
org.springframework.http.converter.HttpMessageConverter

Placeholder HttpMessageConverter type to pass to RequestBodyAdvice if there is no content-type and no content. In that case, we may not find a converter, but RequestBodyAdvice have a chance to provide it via handleEmptyBody.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
canRead(Class<?>
the class to test for readability
clazz
,
MediaType
the media type to read (can be null if not specified); typically the value of a Content-Type header.
mediaType
)

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

Indicates whether the given class can be read by this converter.

public boolean
canWrite(Class<?>
the class to test for writability
clazz
,
MediaType
the media type to write (can be null if not specified); typically the value of an Accept header.
mediaType
)

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

Indicates whether the given class can be written by this converter.

public List<MediaType>
getSupportedMediaTypes()

Implements org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Return the list of media types supported by this converter.

public String
read(Class<? extends String>
the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements org.springframework.http.converter.HttpMessageConverter.read.

Read an object of the given type from the given input message, and returns it.

public void
write(String
the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.
s
,
MediaType
the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.
contentType
,
HttpOutputMessage
the message to write to
outputMessage
)

Implements org.springframework.http.converter.HttpMessageConverter.write.

Write a given object to the given output message.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

NoContentTypeHttpMessageConverterback to summary
private NoContentTypeHttpMessageConverter()

Method Detail

canReadback to summary
public boolean canRead(Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

Indicates whether the given class can be read by this converter.

Parameters
clazz:Class<?>

the class to test for readability

mediaType:MediaType

the media type to read (can be null if not specified); typically the value of a Content-Type header.

Returns:boolean

true if readable; false otherwise

Annotations
@Override
canWriteback to summary
public boolean canWrite(Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

Indicates whether the given class can be written by this converter.

Parameters
clazz:Class<?>

the class to test for writability

mediaType:MediaType

the media type to write (can be null if not specified); typically the value of an Accept header.

Returns:boolean

true if writable; false otherwise

Annotations
@Override
getSupportedMediaTypesback to summary
public List<MediaType> getSupportedMediaTypes()

Implements org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Doc from org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Return the list of media types supported by this converter. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(clazz, null. The list may also exclude MIME types supported only for a specific class. Alternatively, use getSupportedMediaTypes(Class) for a more precise list.

Returns:List<MediaType>

the list of supported media types

Annotations
@Override
readback to summary
public String read(Class<? extends String> clazz, HttpInputMessage inputMessage)

Implements org.springframework.http.converter.HttpMessageConverter.read.

Doc from org.springframework.http.converter.HttpMessageConverter.read.

Read an object of the given type from the given input message, and returns it.

Parameters
clazz:Class<? extends String>

the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:String

the converted object

Annotations
@Override
writeback to summary
public void write(String s, MediaType contentType, HttpOutputMessage outputMessage)

Implements org.springframework.http.converter.HttpMessageConverter.write.

Doc from org.springframework.http.converter.HttpMessageConverter.write.

Write a given object to the given output message.

Parameters
s:String

the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.

contentType:MediaType

the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.

outputMessage:HttpOutputMessage

the message to write to

Annotations
@Override