拦截器HandlerInterceptorAdapter的postHandle和afterCompletion无法获取response返回值问题
缘起
有一个需求,在进入controller之前验证调用次数是否超过限制,在响应之后判断是否正常返回,对调用次数进行+1,发现带@RestController的类和带@ResponseBody的方法在被调用后response会直接写入输出流,在postHandle和afterCompletion这两个方法执行之前就已经把数据返回,导致这两个方法里面的response根本获取不到响应数据(也无法拿到头信息等)。
解决方案
先解释一下为什么不用过滤器,因为这个需求是拦截带某个特定注解的controller方法,还需要获取到这个注解里面的一些数据,因此Filter方案没法满足这样的需求。怎么解决这个问题呢?那就是使用@ControllerAdvice,这个注解标注的类需要实现ResponseBodyAdvice,这样在response返回前会调用这个类的beforeBodyWrite方法,我们就在beforeBodyWrite方法里面做文章,来进行“曲线救国”。代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.lang.reflect.Method;
/**
**/
@ControllerAdvice
public class ApiResponseBody implements ResponseBodyAdvice<RestResult> {
private static final Logger logger = LoggerFactory.getLogger(ApiResponseBody.class);
@Autowired
private RedisClientWrapper redisClientWrapper;
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
Method method = methodParameter.getMethod();
return method.isAnnotationPresent(InvokeLimit.class);
}
@Override
public RestResult beforeBodyWrite(RestResult restResult, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
logger.info("调用限制拦截器进入次数增加");
if(ResultEnums.SUCCESS.getCode().equals(restResult.getCode())){
String appId = methodParameter.getMethod().getAnnotation(InvokeLimit.class).value();
String cusNo = serverHttpRequest.getHeaders().get(BussConstant.INVOKE_HEADER_CUS_NO).get(0);
String times = redisClientWrapper.increment(appId + ":" + BussConstant.INVOKE_NUM_CURRENT_PREFIX + cusNo);
logger.info("调用限制拦截器次数增加完成,cusNo={},appId={},当前次数为:{}",cusNo,appId,times);
}
return restResult;
}
}
supports方法是来给定条件判断是否该调用beforeBodyWrite,MethodParameter里面有各种数据,其中就有我想要的:调用了哪个方法,从而获得标注在上面的注解。beforeBodyWrite中就是我的逻辑,其中也包含MethodParameter,并且还有封装了的request和response,非常灵活。
关键代码:@seeorg.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor#writeWithMessageConverterswriteWithMessageConverters(@Nullable T value, MethodParameter returnType,ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
if (selectedMediaType != null) {
selectedMediaType = selectedMediaType.removeQualityValue();
for (HttpMessageConverter<?> converter : this.messageConverters) {
GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
(GenericHttpMessageConverter<?>) converter : null);
if (genericConverter != null ?
((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :
converter.canWrite(valueType, selectedMediaType)) {
//关键代码
body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,
(Class<? extends HttpMessageConverter<?>>) converter.getClass(),
inputMessage, outputMessage);
if (body != null) {
Object theBody = body;
LogFormatUtils.traceDebug(logger, traceOn ->
"Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");
addContentDispositionHeader(inputMessage, outputMessage);
if (genericConverter != null) {
genericConverter.write(body, targetType, selectedMediaType, outputMessage);
}
else {
((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Nothing to write: null body");
}
}
return;
}
}
}
注:同理还有RequestBodyAdvice-> @RequestBody等