httpservletrequest getbody为空

t1rydlwq  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(330)

我正在实现一个在异常时被调用的服务,这个服务将异常保存在db中。问题在于存储请求的有效负载。因为httpservletrequest正在为从中提取负载提供空字符串

request.getReader.lines()..collect(Collectors.joining())

或者使用contentcachingrequestwrapper。因为控制器在这些实现之前正在耗尽有效负载。有解决这个问题的办法吗。我的课是

@Service
public class ExceptionLogService {
 void save exception(){ 
   ExceptionLog.builder().setBody(request.getReader.lines()
 .collect(Collectors.joining())).build();
 OR
 Exception.builder().setBody(getBody()).build();}
 }

getbody方法

public static String getBody(HttpServletRequest request)  {

        ContentCachingRequestWrapper wrapper =
                WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            byte[] buf = wrapper.getContentAsByteArray();
            if (buf.length > 0) {
                int length = Math.min(buf.length, 1000);
                try {
                    return new String(buf, 0, length, wrapper.getCharacterEncoding());
                }
                catch (UnsupportedEncodingException ex) {
                    return "[unknown]";
                }
            }
        }
        return null;

}

有什么办法可以让尸体服役吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题