拦截器打印入参

  • Post author:
  • Post category:其他




原因

@RequestBody 入参转换成实体报错 需要记录日志溯源



解决方案

  1. 写个包装类,将原请求流中得入参保存,并重新创建流返回(解决了流只能读取一次得问题)

import org.springframework.util.StreamUtils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
 
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
 
/**
 * 
 * 定义增强类,集成HttpServletRequestWrapper,重写getInputStream()
 * 
 * @author YunDuan-Clutch
 * @date 2021年11月24日 上午10:46:47
 *
 */
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
 
    private byte[] body;
 
    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        body = StreamUtils.copyToByteArray(request.getInputStream());
    }
 
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }
 
    @Override
    public ServletInputStream getInputStream() throws IOException {
 
        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
 
        return new ServletInputStream() {
 
            @Override
            public int read() throws IOException {
                return bais.read();
            }
 
            @Override
            public boolean isFinished() {
                return false;
            }
 
            @Override
            public boolean isReady() {
                return false;
            }
 
            @Override
            public void setReadListener(ReadListener readListener) {
 
            }
 
        };
    }
 
    public void setInputStream(byte[] body) {
        this.body = body;
    }
	
}
  1. 将流转换成string或map

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
 
import com.alibaba.fastjson.JSON;
 
import cn.hutool.core.util.StrUtil;
 
/**
 * 获取request中的body参数
 * @author YunDuan-Clutch
 * @date 2021年11月24日 上午10:46:47
 */
public class HttpHelper {
 
	/**
	 * 获取body,json字符串的形式
	 * @author YunDuan-Clutch
	 * @param request
	 * @date 2021年11月24日 上午10:46:47
	 * @return String
	 */
	public static String getBodyString(ServletRequest request) {
        StringBuilder sb = new StringBuilder();
        ServletInputStream inputStream = null;
        BufferedReader reader = null;
        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
 
	/**
	 * 获取body,map的形式
	 * @author YunDuan-Clutch
	 * @param request
	 * @date 2021年11月24日 上午10:48:29
	 * @return Map<String,Object>
	 */
    @SuppressWarnings("all")
    public static Map<String, Object> getBodyMap(ServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        String bodyString = getBodyString(request);
        if (StrUtil.isNotEmpty(bodyString)) {
            params = JSON.parseObject(bodyString, Map.class);
        }
        return params;
    }
}
  1. 拦截器 打印

import cn.ecmax.rpa.domain.entity.CustomOrder;
import cn.ecmax.rpa.domain.entity.RPABusinessInfo;
import cn.ecmax.rpa.domain.entity.RPAErrorLog;
import cn.ecmax.rpa.domain.entity.RPALog;
import cn.ecmax.rpa.mapper.RPAErrorLogMapper;
import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(filterName = "RequestWrapperFilter", urlPatterns = "/*")
@Slf4j
@Component
public class RequestWrapperFilter implements Filter {

    @Autowired
    private RPAErrorLogMapper rpaErrorLogMapper;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException
            , IOException {
        ServletRequest requestWrapper =  new BodyReaderHttpServletRequestWrapper((HttpServletRequest) request);
        HttpServletRequest req = (HttpServletRequest) request;
        String requestUri = URLUtil.getPath(req.getRequestURI());
        String bodyString = HttpHelper.getBodyString(requestWrapper);
        try{
            log.info("入参为:{}",bodyString);
            if("/rpa/sad/pushOrderData".equals(requestUri)){
                List<CustomOrder> list= JSON.parseArray(bodyString,CustomOrder.class);
            }
        }catch(Exception e){
        	// 异常插入数据库
            RPAErrorLog rpaErrorLog=new RPAErrorLog();
            rpaErrorLog.setErrorParam(bodyString);
            rpaErrorLog.setUrl(requestUri);
            rpaErrorLog.setCreateTime(new Date());
            rpaErrorLog.setErrorContent(e.getMessage());
            rpaErrorLogMapper.insert(rpaErrorLog);
        }
        req=(HttpServletRequest)requestWrapper;
        // 过滤器包装request成功
        chain.doFilter(req, response);

    }

    @Override
    public void destroy() {

    }

}



版权声明:本文为qq_33407429原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。