How to get response from HttpServletResponse and save to database in Java via interceptor

552 views Asked by At

I want to save request and response in String format to database when endpoint /v2 is called. This implementation workes fine and save is correct but if i call to v1 endpoint, is muted, response is null, just 200 OK in Postman. Normally it response with json {id="123456789"} Whats wrong have i in this implementation that endpoint v1 doesnt work ? If i delete CachingRequestBodyFilter class, v1 works fine but when v2 is called, nothing saved to database. v1 endpoint is for xml and v2 endpoint is for json format. Meybe is better way to save request and response to db via interceptors?

@AllArgsConstructor
@Component
@Data
public class RequestInterceptor implements HandlerInterceptor {

    private final RequestService requestService;

    @Override
    public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex){
        requestService.saveResponse(request, response, ex);
    }
}


    public void saveResponse(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        try {
            String requestBody = getRequestAsString(request);
            String responseBody = getResponseAsString(response);
            buildMessages(requestBody, responseBody, ex);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getRequestAsString(HttpServletRequest request) throws IOException {
        ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) request;
        return new String(requestWrapper.getContentAsByteArray(), requestWrapper.getCharacterEncoding());
    }


    private String getResponseAsString(HttpServletResponse response) throws IOException {
        ContentCachingResponseWrapper responseWrapper = (ContentCachingResponseWrapper) response;
        byte[] responseArray = responseWrapper.getContentAsByteArray();
        String characterEncoding = responseWrapper.getCharacterEncoding();
        responseWrapper.copyBodyToResponse();
        return new String(responseArray, characterEncoding);
}

method buildMessages() is just a builder object which i want to save to db.

filter class:

@Component
@WebFilter(filterName = "CachingRequestBodyFilter", urlPatterns = {"/v2/*"})
public class CachingRequestBodyFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
                                    FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper(request);

        HttpServletResponse response = (HttpServletResponse) res;
        ContentCachingResponseWrapper wrappedResponse = new ContentCachingResponseWrapper(response);
        filterChain.doFilter(wrappedRequest, wrappedResponse);
    }
}

i want to save my request and response to db when endpoint v2 is called and when v1 is called response is not null;

0

There are 0 answers