001/* 002 * Copyright 2002-2017 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; 024 025/** 026 * Allows customizing the response after the execution of an {@code @ResponseBody} 027 * or a {@code ResponseEntity} controller method but before the body is written 028 * with an {@code HttpMessageConverter}. 029 * 030 * <p>Implementations may be registered directly with 031 * {@code RequestMappingHandlerAdapter} and {@code ExceptionHandlerExceptionResolver} 032 * or more likely annotated with {@code @ControllerAdvice} in which case they 033 * will be auto-detected by both. 034 * 035 * @author Rossen Stoyanchev 036 * @since 4.1 037 */ 038public interface ResponseBodyAdvice<T> { 039 040 /** 041 * Whether this component supports the given controller method return type 042 * and the selected {@code HttpMessageConverter} type. 043 * @param returnType the return type 044 * @param converterType the selected converter type 045 * @return {@code true} if {@link #beforeBodyWrite} should be invoked; 046 * {@code false} otherwise 047 */ 048 boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType); 049 050 /** 051 * Invoked after an {@code HttpMessageConverter} is selected and just before 052 * its write method is invoked. 053 * @param body the body to be written 054 * @param returnType the return type of the controller method 055 * @param selectedContentType the content type selected through content negotiation 056 * @param selectedConverterType the converter type selected to write to the response 057 * @param request the current request 058 * @param response the current response 059 * @return the body that was passed in or a modified (possibly new) instance 060 */ 061 T beforeBodyWrite(T body, MethodParameter returnType, MediaType selectedContentType, 062 Class<? extends HttpMessageConverter<?>> selectedConverterType, 063 ServerHttpRequest request, ServerHttpResponse response); 064 065}