速读摘要文章源自JAVA秀-https://www.javaxiu.com/20603.html
recordCode方法可用于定义在请求响应码为多少的时候才会去记录body,例如可以定义为只有遇到400或500时才记录body,用于错误侦测。过滤器的匹配规则比较简单,如果想要像springmvc那样进行匹配,我们可以使用AntPathMatcher。欢迎加入我的知识星球,一起探讨架构,交流源码。文章源自JAVA秀-https://www.javaxiu.com/20603.html
原文约 3274 字 | 图片 6 张 | 建议阅读 7 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/20603.html
如何优雅记录 http 请求/ 响应数据?
点击关注 ? 芋道源码 文章源自JAVA秀-https://www.javaxiu.com/20603.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/20603.html
#芋道源码文章源自JAVA秀-https://www.javaxiu.com/20603.html
28个文章源自JAVA秀-https://www.javaxiu.com/20603.html
点击上方“芋道源码”,选择“设为星标”文章源自JAVA秀-https://www.javaxiu.com/20603.html
管她前浪,还是后浪?文章源自JAVA秀-https://www.javaxiu.com/20603.html
能浪的浪,才是好浪!文章源自JAVA秀-https://www.javaxiu.com/20603.html
每天 8:55 更新文章,每天掉亿点点头发...文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html 源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/20603.html
原创 | Java 2020 超神之路,很肝~文章源自JAVA秀-https://www.javaxiu.com/20603.html
中文详细注释的开源项目文章源自JAVA秀-https://www.javaxiu.com/20603.html
RPC 框架 Dubbo 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
网络应用框架 Netty 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
消息中间件 RocketMQ 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
数据库中间件 Sharding-JDBC 和 MyCAT 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
作业调度中间件 Elastic-Job 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
分布式事务中间件 TCC-Transaction 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
Eureka 和 Hystrix 源码解析文章源自JAVA秀-https://www.javaxiu.com/20603.html
Java 并发源码文章源自JAVA秀-https://www.javaxiu.com/20603.html
来源:fredal.xin/http-body-recorder文章源自JAVA秀-https://www.javaxiu.com/20603.html
我们编写一个过滤器:文章源自JAVA秀-https://www.javaxiu.com/20603.html
PatternMappingFilterProxy文章源自JAVA秀-https://www.javaxiu.com/20603.html
测试文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
经常会遇到需要处理http请求以及响应body的场景。而这里比较大的一个问题是servlet的requestBody或responseBody流一旦被读取了。就无法二次读取了。针对这个问题,spring本身提供了解决方案,即ContentCachingRequestWrapper/ContentCachingResponseWrapper。文章源自JAVA秀-https://www.javaxiu.com/20603.html
我们编写一个过滤器:
public abstract class HttpBodyRecorderFilter extends OncePerRequestFilter { private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 1024 * 512; private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { boolean isFirstRequest = !isAsyncDispatch(request); HttpServletRequest requestToUse = request; if (isFirstRequest && !(request instanceof ContentCachingRequestWrapper) && (request.getMethod().equals(HttpMethod.PUT.name()) || request.getMethod().equals(HttpMethod.POST.name()))) { requestToUse = new ContentCachingRequestWrapper(request); } HttpServletResponse responseToUse = response; if (!(response instanceof ContentCachingResponseWrapper) && (request.getMethod().equals(HttpMethod.PUT.name()) || request.getMethod().equals(HttpMethod.POST.name()))) { responseToUse = new ContentCachingResponseWrapper(response); } boolean hasException = false; try { filterChain.doFilter(requestToUse, responseToUse); } catch (final Exception e) { hasException = true; throw e; } finally { int code = hasException ? 500 : response.getStatus(); if (!isAsyncStarted(requestToUse) && (this.codeMatched(code, AdvancedHunterConfigManager.recordCode()))) { recordBody(createRequest(requestToUse), createResponse(responseToUse)); } else { writeResponseBack(responseToUse); } } } protected String createRequest(HttpServletRequest request) { String payload = ""; ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); payload = genPayload(payload, buf, wrapper.getCharacterEncoding()); } return payload; } protected String createResponse(HttpServletResponse resp) { String response = ""; ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(resp, ContentCachingResponseWrapper.class); if (wrapper != null) { byte[] buf = wrapper.getContentAsByteArray(); try { wrapper.copyBodyToResponse(); } catch (IOException e) { e.printStackTrace(); } response = genPayload(response, buf, wrapper.getCharacterEncoding()); } return response; } protected void writeResponseBack(HttpServletResponse resp) { ContentCachingResponseWrapper wrapper = WebUtils.getNativeResponse(resp, ContentCachingResponseWrapper.class); if (wrapper != null) { try { wrapper.copyBodyToResponse(); } catch (IOException e) { LOG.error("Fail to write response body back", e); } } } private String genPayload(String payload, byte[] buf, String characterEncoding) { if (buf.length > 0 && buf.length < getMaxPayloadLength()) { try { payload = new String(buf, 0, buf.length, characterEncoding); } catch (UnsupportedEncodingException ex) { payload = "[unknown]"; } } return payload; } public int getMaxPayloadLength() { return maxPayloadLength; } private boolean codeMatched(int responseStatus, String statusCode) { if (statusCode.matches("^[0-9,]*$")) { String[] filteredCode = statusCode.split(","); return Stream.of(filteredCode).map(Integer::parseInt).collect(Collectors.toList()).contains(responseStatus); } else { return false; } } protected abstract void recordBody(String payload, String response); protected abstract String recordCode();}
这样自定义一个filter继承HttpBodyRecorderFilter,重写recordBody方法就能自定义自己的处理逻辑了。另外,recordCode方法可用于定义在请求响应码为多少的时候才会去记录body,例如可以定义为只有遇到400或500时才记录body,用于错误侦测。文章源自JAVA秀-https://www.javaxiu.com/20603.html
PatternMappingFilterProxy
过滤器的匹配规则比较简单,如果想要像springmvc那样进行匹配,我们可以使用 AntPathMatcher 。文章源自JAVA秀-https://www.javaxiu.com/20603.html
class PatternMappingFilterProxy implements Filter { private final Filter delegate; private final List<String> pathUrlPatterns = new ArrayList(); private PathMatcher pathMatcher; public PatternMappingFilterProxy(Filter delegate, String... urlPatterns) { Assert.notNull(delegate, "A delegate Filter is required"); this.delegate = delegate; int length = urlPatterns.length; pathMatcher = new AntPathMatcher(); for (int index = 0; index < length; ++index) { String urlPattern = urlPatterns[index]; this.pathUrlPatterns.add(urlPattern); } } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String path = httpRequest.getRequestURI(); if (this.matches(path)) { this.delegate.doFilter(request, response, filterChain); } else { filterChain.doFilter(request, response); } } private boolean matches(String requestPath) { for (String pattern : pathUrlPatterns) { if (pathMatcher.match(pattern, requestPath)) { return true; } } return false; } @Override public void init(FilterConfig filterConfig) throws ServletException { this.delegate.init(filterConfig); } @Override public void destroy() { this.delegate.destroy(); } public List<String> getPathUrlPatterns() { return pathUrlPatterns; } public void setPathUrlPatterns(List<String> urlPatterns) { pathUrlPatterns.clear(); pathUrlPatterns.addAll(urlPatterns); }}
测试
这样子,PatternMappingFilterProxy装饰了真正的HttpBodyRecorderFilter,支持传入urlPatterns,从而实现像springmvc那样的ant style的匹配。例如对于以下接口:文章源自JAVA秀-https://www.javaxiu.com/20603.html
@PostMapping("/test/{id}")public Object test(@PathVariable(value = "id",required = true) final Integer index) { //do something }
可以设置urlPattern为/test/{id:[0-9]+}
。文章源自JAVA秀-https://www.javaxiu.com/20603.html
以上代码存在于httpBodyRecorder。文章源自JAVA秀-https://www.javaxiu.com/20603.html
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/20603.html
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/20603.html
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章源自JAVA秀-https://www.javaxiu.com/20603.html
文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/20603.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/20603.html

评论