zoukankan      html  css  js  c++  java
  • Springboot 请求参数通知RequestBodyAdvice和响应通知ResponseBodyAdvice做日志的打印

    1,请求参数的打印

    @ControllerAdvice(basePackages = "控制器的命名空间")
    public class LogRequestAdvice implements RequestBodyAdvice {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
    //判断是否有此注解
    boolean b = methodParameter.getParameterAnnotation(RequestBody.class) != null;
    //只有为true时才会执行afterBodyRead
    return b;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
    InputStream body = httpInputMessage.getBody();
    return new MappingJacksonInputMessage(httpInputMessage.getBody(), httpInputMessage.getHeaders());
    }

    @Override
    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
    //仅仅打印请求参数
    String s = JSON.toJSONString(o);
    /**
    * 去除注 回车 水平制表符 空格 s 换行
    */
    String s2 = StringUtils.replaceBlankByLow(s);
    logger.info("请求参数为:{}", s2);
    return o;
    }

    @Override
    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
    return null;
    }
    2,请求参数的打印
    @ControllerAdvice(basePackages = "控制器的命名空间")
    public class LogResponseAdvice implements ResponseBodyAdvice<Object> {

    Logger logger = LoggerFactory.getLogger(LogResponseAdvice.class);

    @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) {
    /**
    * 仅仅打印返回的参数参数
    */
    String s = JSON.toJSONString(body);
    logger.info("返回值为:{}", s);
    return body;
    }


  • 相关阅读:
    正向代理/反向代理理解、Nginx概述、安装及配置详解
    项目部署问题:xftp无法连接服务器、Nginx403 Forbidden解决、nginx反向代理解决前端跨域问题
    Vue上传文件:ElementUI中的upload实现
    理解Vue的计算属性
    今天在CSDN看懂这个帖子,也是我的困惑,记录一下(过了三十的码农,你选择的是哪个,说出你的想法)
    WCF IIS上部署服务
    [转]WCF RESTful service and WebGrid in ASP.NET MVC 5
    WCF 与其它技术的比较
    Visual Studio Debug和Release的区别及obj的作用
    C# Json格式字符串
  • 原文地址:https://www.cnblogs.com/guagua-join-1/p/10368533.html
Copyright © 2011-2022 走看看