import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.http.server.ServletServerHttpRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import cc.xxxxx.common.retus.ReturnMsg; /** * 日志跟踪记录接口参数及返回状态 * * @fileName CatResponseBodyAdvice.java * @Description * @author light-zhang * @date 2018年6月26日下午2:55:55 * @version 1.0.0 */ @Controller @ControllerAdvice public class CatResponseBodyAdvice implements ResponseBodyAdvice<ReturnMsg<?>> { private static final Logger logger = LoggerFactory.getLogger(CatResponseBodyAdvice.class); private static ServletServerHttpRequest servletRequest; @Override public ReturnMsg<?> beforeBodyWrite(ReturnMsg<?> arg0, MethodParameter arg1, MediaType arg2, Class<? extends HttpMessageConverter<?>> arg3, ServerHttpRequest arg4, ServerHttpResponse arg5) { servletRequest = (ServletServerHttpRequest) arg4; logger.info("访问者ip地址信息:{}", arg4.getRemoteAddress().getHostString()); logger.info("协议{},方式:{},请求地址信息:{}", arg4.getURI().getScheme(), arg4.getMethodValue(), arg4.getURI().toASCIIString()); logger.info("请求CLASS-路径信息:{}", arg1.getExecutable().getDeclaringClass().getName()); logger.info("请求函数名称:{},请求媒体类型信息:{}", arg1.getExecutable().getName(), arg2.toString()); logger.info("请求参数信息:{}", servletRequest.getServletRequest().getParameterMap()); logger.info("响应参数信息:{}", arg0); return arg0; } @Override public boolean supports(MethodParameter arg0, Class<? extends HttpMessageConverter<?>> arg1) { return true; } }