不喜欢讲废话直接正题,
在开发阶段我们可能需要对所有的出参和入参打印log
在每个方法上都自己手动操作过于麻烦,下面介绍一个省事的办法。
自己封装request 和response
spring boot 提供了两个包装类型分别是:HttpServletRequestWrapper和HttpServletResponseWrapper
HttpServletRequestWrapper实例:
public class MyRequestWrapper extends HttpServletRequestWrapper {
private byte[] requestBody=null;
@SneakyThrows
public MyRequestWrapper(HttpServletRequest request) {
super(request);
String body = HttpUtils.getBody(request);
String url = request.getRequestURI();
log.info("访问路径{} 方法入参: {}",url,body);
requestBody=body.getBytes(Charset.defaultCharset());
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream inputStream=new ByteArrayInputStream(requestBody);
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
return inputStream.read();
}
};
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
public byte[] getRequestBody(){
return this.requestBody;
}
}
大家都知道request body 只能取一次,所以这里的主要包装是用于将请求体数据自己保存起来可以多次使用!
HttpServletResponseWrapper示例:
public class ResponseWrapper extends HttpServletResponseWrapper {
ByteArrayOutputStream output;
public ResponseWrapper(HttpServletResponse response) {
super(response);
output = new ByteArrayOutputStream();
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener listener) {
}
@Override
public void write(int b) throws IOException {
output.write(b);
}
@Override
public void write(byte[] b) throws IOException {
output.write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
output.write(b,off,len);
}
};
}
public byte[] getDataStream() {
return output.toByteArray();
}
}
这里主要把response的数据用字节流读取保存,目的是一样的由于字节流的特性可以多次使用
封装完两个类之后开始配置Filter 主要目的是把原来的request和response换成我们封装的
示例:
@Component
@Slf4j
@WebFilter(filterName = "channelFilter", urlPatterns = {"/*"})
public class ChannelFilter implements Filter {
@Override
/**
* @author: Mir Dai
* @description: 用自己封装request 和response入参
* @date: 2021/5/20 15:39
* @param request
* @param response
* @param chain
* @return {@link Void}
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain){
try {
ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) response);
ServletRequest requestWrapper = new MyRequestWrapper((HttpServletRequest) request);
chain.doFilter(requestWrapper,responseWrapper);
String s = new String(responseWrapper.getDataStream());
String requestURI = ((HttpServletRequest) request).getRequestURI();
log.info("访问路径{} 方法出参: {}",requestURI,s);
response.getOutputStream().write(s.getBytes());
}catch (Exception e){
e.getMessage();
log.error(e.getMessage());
}
}
}
这里有个细节,我们是用我们封装的response去读取我们业务返回的数据 然后用原来的response去返回数据
如果想更完善一点 可以把我们封装的response 所有请求头取出给原生的response头全部换掉再返回
这样我们的日志入参出参请求就完成了
有问题就是插入的姿势不对!