package com.zhqc.cloud.third.config;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* @author zdd
*/
@RestControllerAdvice
@Slf4j
public class ResponseResultAdvice implements ResponseBodyAdvice<Object> {
// return true表示对响应进行处理,你可以用returnType来做判断什么时候不需要做处理
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
// 响应报文打印是最常用的
log.info("--result response start--");
log.info("调用方法:{}", returnType.getMethod().getName());
log.info("响应报文:{}", JSON.toJSONString(body));
log.info("--result response end--");
// 可对响应报文做其他操作,这里不举例
return body;
}
}
拦截获取接口响应数据
最新推荐文章于 2024-03-21 15:34:30 发布