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

public Interface ResponseBodyAdvice<T>

Known Direct Implementers
org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice, org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyAdviceChain
Type Parameters
<T>
the body type
Imports
org.springframework.core.MethodParameter, org.springframework.http.MediaType, org.springframework.http.converter.HttpMessageConverter, org.springframework.http.server.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable

Allows customizing the response after the execution of an @ResponseBody or a ResponseEntity controller method but before the body is written with an HttpMessageConverter.

Implementations may be registered directly with RequestMappingHandlerAdapter and ExceptionHandlerExceptionResolver or more likely annotated with @ControllerAdvice in which case they will be auto-detected by both.

Author
Rossen Stoyanchev
Since
4.1

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

the body that was passed in or a modified (possibly new) instance
beforeBodyWrite
(T
the body to be written
body
,
MethodParameter
the return type of the controller method
returnType
,
MediaType
the content type selected through content negotiation
selectedContentType
,
Class<? extends HttpMessageConverter<?>>
the converter type selected to write to the response
selectedConverterType
,
ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
)

Invoked after an HttpMessageConverter is selected and just before its write method is invoked.

public boolean

Returns:

true if beforeBodyWrite should be invoked; false otherwise
supports
(MethodParameter
the return type
returnType
,
Class<? extends HttpMessageConverter<?>>
the selected converter type
converterType
)

Whether this component supports the given controller method return type and the selected HttpMessageConverter type.

Method Detail

beforeBodyWriteback to summary
public T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response)

Invoked after an HttpMessageConverter is selected and just before its write method is invoked.

Parameters
body:T

the body to be written

returnType:MethodParameter

the return type of the controller method

selectedContentType:MediaType

the content type selected through content negotiation

selectedConverterType:Class<? extends HttpMessageConverter<?>>

the converter type selected to write to the response

request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

Returns:T

the body that was passed in or a modified (possibly new) instance

Annotations
@Nullable
supportsback to summary
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType)

Whether this component supports the given controller method return type and the selected HttpMessageConverter type.

Parameters
returnType:MethodParameter

the return type

converterType:Class<? extends HttpMessageConverter<?>>

the selected converter type

Returns:boolean

true if beforeBodyWrite should be invoked; false otherwise