001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.mvc.method.annotation;
018
019import org.springframework.core.MethodParameter;
020import org.springframework.http.MediaType;
021import org.springframework.http.converter.HttpMessageConverter;
022import org.springframework.http.server.ServerHttpRequest;
023import org.springframework.http.server.ServerHttpResponse;
024import org.springframework.lang.Nullable;
025
026/**
027 * Allows customizing the response after the execution of an {@code @ResponseBody}
028 * or a {@code ResponseEntity} controller method but before the body is written
029 * with an {@code HttpMessageConverter}.
030 *
031 * <p>Implementations may be registered directly with
032 * {@code RequestMappingHandlerAdapter} and {@code ExceptionHandlerExceptionResolver}
033 * or more likely annotated with {@code @ControllerAdvice} in which case they
034 * will be auto-detected by both.
035 *
036 * @author Rossen Stoyanchev
037 * @since 4.1
038 * @param <T> the body type
039 */
040public interface ResponseBodyAdvice<T> {
041
042        /**
043         * Whether this component supports the given controller method return type
044         * and the selected {@code HttpMessageConverter} type.
045         * @param returnType the return type
046         * @param converterType the selected converter type
047         * @return {@code true} if {@link #beforeBodyWrite} should be invoked;
048         * {@code false} otherwise
049         */
050        boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
051
052        /**
053         * Invoked after an {@code HttpMessageConverter} is selected and just before
054         * its write method is invoked.
055         * @param body the body to be written
056         * @param returnType the return type of the controller method
057         * @param selectedContentType the content type selected through content negotiation
058         * @param selectedConverterType the converter type selected to write to the response
059         * @param request the current request
060         * @param response the current response
061         * @return the body that was passed in or a modified (possibly new) instance
062         */
063        @Nullable
064        T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,
065                        Class<? extends HttpMessageConverter<?>> selectedConverterType,
066                        ServerHttpRequest request, ServerHttpResponse response);
067
068}