如何在java中获取失败的HTTP请求正文?

奥古占切维克

我想记录所有 HTTP 请求。

通常,我可以使用以下代码获取成功请求的请求正文。

但是如果请求失败,我不会得到请求正文。

当我尝试获取失败请求的正文时,我只得到一个空字符串。

如何在java中获取失败的HTTP请求正文?

-过滤器类-

@Component
@Slf4j
public class HttpLogFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        long startTime = System.currentTimeMillis();
        String requestId = UUID.randomUUID().toString();
        response.addHeader(REQUEST_ID_PROPERTY_NAME, requestId);
        ThreadContext.put(REQUEST_ID_PROPERTY_NAME, requestId);

        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);

        filterChain.doFilter(requestWrapper, responseWrapper);

        HttpLogDTO httpLog = collectHttpInfo(requestWrapper, responseWrapper);

        responseWrapper.copyBodyToResponse();

        long endTime = System.currentTimeMillis();
        httpLog.setDuration(endTime - startTime);
        log.info(httpLog.toString());
    }

    private HttpLogDTO collectHttpInfo(HttpServletRequest request, HttpServletResponse response) {
        String scheme = request.getScheme();
        String host = request.getServerName();
        String port = String.valueOf(request.getLocalPort());
        String path = request.getServletPath();
        String parameters = request.getParameterMap().keySet().stream()
                .map(key -> key + "=" + request.getParameterMap().get(key)[0]).collect(Collectors.joining("&"));

        Map<String, List<String>> requestHeaders = Collections.list(request.getHeaderNames())
                .stream()
                .collect(Collectors.toMap(
                        Function.identity(),
                        h -> Collections.list(request.getHeaders(h))
                ));

        Map<String, String> responseHeaders = response.getHeaderNames()
                .stream()
                .collect(Collectors.toMap(h -> h, response::getHeader));

        return HttpLogDTO.builder()
                .protocol(request.getProtocol())
                .remote(request.getRemoteAddr())
                .method(request.getMethod())
                .uri(scheme + "://" + host + ":" + port + path + (StringUtils.isNotBlank(parameters) ? "?" + parameters : ""))
                .host(host)
                .path(path)
                .scheme(scheme)
                .port(port)
                .requestHeaders(requestHeaders.toString())
                .requestBody(new String(((ContentCachingRequestWrapper) request).getContentAsByteArray()))
                .statusCode(String.valueOf(response.getStatus()))
                .statusValue(Objects.requireNonNull(HttpStatus.resolve(response.getStatus())).getReasonPhrase())
                .responseHeaders(responseHeaders.toString())
                .responseBody(new String(((ContentCachingResponseWrapper) response).getContentAsByteArray()))
                .build();
    }

}
奥古占切维克

[解决了]

问题不在上面的 Filter 类中。

问题是我确实编写了代码,就像我不希望控制器类中的 body 一样。

在方法中添加带有 @RequestBody 注释的 POJO 类后问题解决了。

旧代码

@PostMapping("/post")
public ResponseEntity post() {
    throw new BusinessValidationException(BusinessValidationRule.CITY_NOT_FOUND);
}

新代码

@PostMapping("/post")
public ResponseEntity post(@RequestBody Object body) {
    throw new BusinessValidationException(BusinessValidationRule.CITY_NOT_FOUND);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当Java中的请求失败时,如何获取http响应正文?

如何在Laravel中获取HTTP请求正文内容?

如何在JMETER中打印或获取HTTP POST请求正文

如何在Java REST保证的HTTP请求中打印请求正文和标头?

如何在Spring Boot Filter中获取HTTP请求正文内容?

如何在Google Cloud Functions中获取HTTP请求正文大小?

如何在Node.js HTTP2中获取请求正文?

如何在 Beanshell 预处理器中获取 HTTP POST 请求正文?

如何仅从GET HTTP请求中获取消息正文?

如何在Clojure中从HTTP请求中提取正文?

如何在Matlab中设置http发布请求的正文

如何在我的删除HTTP请求中添加正文

如何在Google Cloud Function中获取原始请求正文?

如何在 Play 中获取原始请求正文?

在使用JAVA的Azure函数中,如何获取请求正文

如何在HTTP请求中获取数组

如何使用 httptools 从请求中获取正文?

如何在Java中的HTTP帖子正文中添加json

如何在芬奇中绑定请求正文

如何在 Koa 中打印请求正文

如何在 Elasticsearch 中减少请求正文

如何在Go http服务器中解析http Get请求正文?

如何在单元测试中验证Flurl Http中的请求正文内容?

如何使用Node.js中的请求库获取原始HTTP消息正文?

如何在php中的Curl请求中仅获取响应的正文

如何在Java中将HTTP请求正文转换为JSON对象

如何在同一Web服务中获取@PathParam和请求正文?

如何在Hapijs中获取原始请求正文的缓冲区?

如何在Slim中访问POST请求的JSON请求正文?