Can not get request body in “Interceptor” layer of SpringBoot project [duplicate]

I’m trying to get request body of request and sending to database
Here is my lines of code :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Component
public class CustomInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(CustomInterceptor.class);
private final LogStore logStore;
public CustomInterceptor(LogStore logStore) {
this.logStore = logStore;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// Wrap the request with ContentCachingRequestWrapper
ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);
// Log request details
Log requestLog = new Log();
requestLog.setPath(wrappedRequest.getRequestURI());
requestLog.setMethod(wrappedRequest.getMethod());
String requestBody = getRequestBody(wrappedRequest);
requestLog.setRequestObject(requestBody);
// Save request log to database
logStore.save(requestLog);
return true; // Proceed with handler execution
}
private String getRequestBody(ContentCachingRequestWrapper request) {
byte[] buf = request.getContentAsByteArray();
if (buf.length > 0) {
return new String(buf, 0, buf.length, StandardCharsets.UTF_8);
}
return "";
}
}
</code>
<code>@Component public class CustomInterceptor implements HandlerInterceptor { private static final Logger log = LoggerFactory.getLogger(CustomInterceptor.class); private final LogStore logStore; public CustomInterceptor(LogStore logStore) { this.logStore = logStore; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // Wrap the request with ContentCachingRequestWrapper ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request); // Log request details Log requestLog = new Log(); requestLog.setPath(wrappedRequest.getRequestURI()); requestLog.setMethod(wrappedRequest.getMethod()); String requestBody = getRequestBody(wrappedRequest); requestLog.setRequestObject(requestBody); // Save request log to database logStore.save(requestLog); return true; // Proceed with handler execution } private String getRequestBody(ContentCachingRequestWrapper request) { byte[] buf = request.getContentAsByteArray(); if (buf.length > 0) { return new String(buf, 0, buf.length, StandardCharsets.UTF_8); } return ""; } } </code>
@Component
public class CustomInterceptor implements HandlerInterceptor  {
    private static final Logger log = LoggerFactory.getLogger(CustomInterceptor.class);
    private final LogStore logStore;

    public CustomInterceptor(LogStore logStore) {
        this.logStore = logStore;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // Wrap the request with ContentCachingRequestWrapper
        ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);

        // Log request details
        Log requestLog = new Log();
        requestLog.setPath(wrappedRequest.getRequestURI());
        requestLog.setMethod(wrappedRequest.getMethod());
        String requestBody = getRequestBody(wrappedRequest);
        requestLog.setRequestObject(requestBody);

        // Save request log to database
        logStore.save(requestLog);

        return true; // Proceed with handler execution
    }

    private String getRequestBody(ContentCachingRequestWrapper request) {
        byte[] buf = request.getContentAsByteArray();
        if (buf.length > 0) {
            return new String(buf, 0, buf.length, StandardCharsets.UTF_8);
        }
        return "";
    }
}

But I always get an empty request body,even though I send a Post method with a JSON request body, what should I do to solve this problem ?
Thanks a lot

2

According to this thread: Get the POST request body from HttpServletRequest it is possible to get the requestBody like this:

String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));

I tested this with your code and it should work.

Something seems to be wrong with your getRequestBody method or with the ContentCachingRequestWrapper that has certain conditions when to return the content.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật