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

public abstract Class AbstractMessageConverterMethodProcessor

extends AbstractMessageConverterMethodArgumentResolver
implements HandlerMethodReturnValueHandler
Class Inheritance
All Implemented Interfaces
org.springframework.web.method.support.HandlerMethodReturnValueHandler
Known Direct Subclasses
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor, org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor
Imports
java.io.IOException, java.lang.reflect.Type, java.util.ArrayList, .Arrays, .Collections, .HashSet, .LinkedHashSet, .List, .Locale, .Set, jakarta.servlet.ServletRequest, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.core.GenericTypeResolver, .MethodParameter, .ParameterizedTypeReference, .ResolvableType, org.springframework.core.io.InputStreamResource, .Resource, org.springframework.core.io.support.ResourceRegion, org.springframework.core.log.LogFormatUtils, org.springframework.http.HttpEntity, .HttpHeaders, .HttpOutputMessage, .HttpRange, .HttpStatus, .MediaType, .MediaTypeFactory, .ProblemDetail, org.springframework.http.converter.GenericHttpMessageConverter, .HttpMessageConverter, .HttpMessageNotWritableException, org.springframework.http.server.ServletServerHttpRequest, .ServletServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .MimeTypeUtils, .StringUtils, org.springframework.web.HttpMediaTypeNotAcceptableException, org.springframework.web.accept.ContentNegotiationManager, org.springframework.web.context.request.NativeWebRequest, .ServletWebRequest, org.springframework.web.method.support.HandlerMethodReturnValueHandler, org.springframework.web.servlet.HandlerMapping, org.springframework.web.util.UrlPathHelper

Extends AbstractMessageConverterMethodArgumentResolver with the ability to handle method return values by writing to the response with HttpMessageConverters.
Authors
Arjen Poutsma, Rossen Stoyanchev, Brian Clozel, Juergen Hoeller
Since
3.1

Field Summary

Modifier and TypeField and Description
private static final List<MediaType>
private final ContentNegotiationManager
private final List<MediaType>
private static final Type
private static final Set<String>
private static final Set<String>
private final Set<String>
Inherited from org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver:
loggermessageConverters

Constructor Summary

AccessConstructor and Description
protected
AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters)

Constructor with list of converters only.

protected
AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, ContentNegotiationManager contentNegotiationManager)

Constructor with list of converters and ContentNegotiationManager.

protected
AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, ContentNegotiationManager manager, List<Object> requestResponseBodyAdvice)

Constructor with list of converters and ContentNegotiationManager as well as request/response body advice instances.

Method Summary

Modifier and TypeMethod and Description
private void
addContentDispositionHeader(ServletServerHttpRequest request, ServletServerHttpResponse response)

Check if the path has a file extension and whether the extension is either on the list of safe extensions or explicitly registered.

protected ServletServerHttpResponse

Returns:

the output message
createOutputMessage
(NativeWebRequest
the web request to create an output message from
webRequest
)

Creates a new HttpOutputMessage from the given NativeWebRequest.

private void
determineCompatibleMediaTypes(List<MediaType> acceptableTypes, List<MediaType> producibleTypes, List<MediaType> mediaTypesToUse)

private List<MediaType>
private Type
getGenericType(MethodParameter returnType)

Return the generic type of the returnType (or of the nested type if it is an HttpEntity).

private MediaType
getMostSpecificMediaType(MediaType acceptType, MediaType produceType)

Return the more specific of the acceptable and the producible media types with the q-value of the former.

protected List<MediaType>
getProducibleMediaTypes(HttpServletRequest request, Class<?> valueClass)

Returns the media types that can be produced.

protected List<MediaType>
getProducibleMediaTypes(HttpServletRequest request, Class<?> valueClass, Type targetType)

Returns the media types that can be produced.

protected Class<?>
getReturnValueType(Object value, MethodParameter returnType)

Return the type of the value to be written to the response.

protected boolean
isResourceType(Object value, MethodParameter returnType)

Return whether the returned value or the declared return type extends Resource.

private MediaType
private boolean
private boolean
protected <T> void
writeWithMessageConverters(T value, MethodParameter returnType, NativeWebRequest webRequest)

Writes the given return value to the given web request.

protected <T> void
writeWithMessageConverters(T
the value to write to the output message
value
,
MethodParameter
the type of the value
returnType
,
ServletServerHttpRequest
the input messages. Used to inspect the Accept header.
inputMessage
,
ServletServerHttpResponse
the output message to write to
outputMessage
)

Writes the given return type to the given output message.

Inherited from org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver:
adaptArgumentIfNecessarycloseStreamIfNecessarycreateInputMessagegetAdvicegetSupportedMediaTypesisBindExceptionRequiredreadWithMessageConvertersreadWithMessageConvertersvalidateIfApplicable

Field Detail

ALL_APPLICATION_MEDIA_TYPESback to summary
private static final List<MediaType> ALL_APPLICATION_MEDIA_TYPES
contentNegotiationManagerback to summary
private final ContentNegotiationManager contentNegotiationManager
problemMediaTypesback to summary
private final List<MediaType> problemMediaTypes
RESOURCE_REGION_LIST_TYPEback to summary
private static final Type RESOURCE_REGION_LIST_TYPE
SAFE_EXTENSIONSback to summary
private static final Set<String> SAFE_EXTENSIONS
SAFE_MEDIA_BASE_TYPESback to summary
private static final Set<String> SAFE_MEDIA_BASE_TYPES
safeExtensionsback to summary
private final Set<String> safeExtensions

Constructor Detail

AbstractMessageConverterMethodProcessorback to summary
protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters)

Constructor with list of converters only.

AbstractMessageConverterMethodProcessorback to summary
protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, ContentNegotiationManager contentNegotiationManager)

Constructor with list of converters and ContentNegotiationManager.

AbstractMessageConverterMethodProcessorback to summary
protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters, ContentNegotiationManager manager, List<Object> requestResponseBodyAdvice)

Constructor with list of converters and ContentNegotiationManager as well as request/response body advice instances.

Method Detail

addContentDispositionHeaderback to summary
private void addContentDispositionHeader(ServletServerHttpRequest request, ServletServerHttpResponse response)

Check if the path has a file extension and whether the extension is either on the list of safe extensions or explicitly registered. If not, and the status is in the 2xx range, a 'Content-Disposition' header with a safe attachment file name ("f.txt") is added to prevent RFD exploits.

createOutputMessageback to summary
protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequest)

Creates a new HttpOutputMessage from the given NativeWebRequest.

Parameters
webRequest:NativeWebRequest

the web request to create an output message from

Returns:ServletServerHttpResponse

the output message

determineCompatibleMediaTypesback to summary
private void determineCompatibleMediaTypes(List<MediaType> acceptableTypes, List<MediaType> producibleTypes, List<MediaType> mediaTypesToUse)
getAcceptableMediaTypesback to summary
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request) throws HttpMediaTypeNotAcceptableException
getGenericTypeback to summary
private Type getGenericType(MethodParameter returnType)

Return the generic type of the returnType (or of the nested type if it is an HttpEntity).

getMostSpecificMediaTypeback to summary
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType)

Return the more specific of the acceptable and the producible media types with the q-value of the former.

getProducibleMediaTypesback to summary
protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Class<?> valueClass)

Returns the media types that can be produced.

Annotations
@SuppressWarnings:unused
See Also
getProducibleMediaTypes(HttpServletRequest, Class, Type)
getProducibleMediaTypesback to summary
protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Class<?> valueClass, Type targetType)

Returns the media types that can be produced. The resulting media types are:

  • The producible media types specified in the request mappings, or
  • Media types of configured converters that can write the specific return value, or
  • MediaType#ALL
Annotations
@SuppressWarnings:unchecked
Since
4.2
getReturnValueTypeback to summary
protected Class<?> getReturnValueType(Object value, MethodParameter returnType)

Return the type of the value to be written to the response. Typically this is a simple check via getClass on the value but if the value is null, then the return type needs to be examined possibly including generic type determination (e.g. ResponseEntity<T>).

isResourceTypeback to summary
protected boolean isResourceType(Object value, MethodParameter returnType)

Return whether the returned value or the declared return type extends Resource.

resolveMediaTypeback to summary
private MediaType resolveMediaType(ServletRequest request, String extension)
Annotations
@Nullable
safeExtensionback to summary
private boolean safeExtension(HttpServletRequest request, String extension)
Annotations
@SuppressWarnings:unchecked
safeMediaTypeback to summary
private boolean safeMediaType(MediaType mediaType)
writeWithMessageConvertersback to summary
protected <T> void writeWithMessageConverters(T value, MethodParameter returnType, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException

Writes the given return value to the given web request. Delegates to writeWithMessageConverters(Object, MethodParameter, ServletServerHttpRequest, ServletServerHttpResponse)

writeWithMessageConvertersback to summary
protected <T> void writeWithMessageConverters(T value, MethodParameter returnType, ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException

Writes the given return type to the given output message.

Parameters
value:T

the value to write to the output message

returnType:MethodParameter

the type of the value

inputMessage:ServletServerHttpRequest

the input messages. Used to inspect the Accept header.

outputMessage:ServletServerHttpResponse

the output message to write to

Annotations
@SuppressWarnings:rawtypes, unchecked
Exceptions
IOException:
thrown in case of I/O errors
HttpMediaTypeNotAcceptableException:
thrown when the conditions indicated by the Accept header on the request cannot be met by the message converters
HttpMessageNotWritableException:
thrown if a given message cannot be written by a converter, or if the content-type chosen by the server has no compatible converter.