Reading httprequest content from spring exception handler

后端 未结 3 608
野性不改
野性不改 2021-02-01 23:36

I Am using Spring\'s @ExceptionHandler annotation to catch exceptions in my controllers.

Some requests hold POST data as plain XML string written to the req

3条回答
  •  情话喂你
    2021-02-01 23:53

    I had the same problem and solved it with HttpServletRequestWrapper as described above and it worked great. But then, I found another solution with extending HttpMessageConverter, in my case that was MappingJackson2HttpMessageConverter.

    public class CustomJsonHttpMessageConverter extends  MappingJackson2HttpMessageConverter{
    
        public static final String REQUEST_BODY_ATTRIBUTE_NAME = "key.to.requestBody";
    
    
        @Override
        public Object read(Type type, Class contextClass, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    
            final ByteArrayOutputStream writerStream = new ByteArrayOutputStream();
    
            HttpInputMessage message = new HttpInputMessage() {
                @Override
                public HttpHeaders getHeaders() {
                    return inputMessage.getHeaders();
                }
                @Override
                public InputStream getBody() throws IOException {
                    return new TeeInputStream(inputMessage.getBody(), writerStream);
                }
            };
                        RequestContextHolder.getRequestAttributes().setAttribute(REQUEST_BODY_ATTRIBUTE_NAME, writerStream, RequestAttributes.SCOPE_REQUEST);
    
            return super.read(type, contextClass, message);
        }
    
    }
    

    com.sun.xml.internal.messaging.saaj.util.TeeInputStream is used.

    In spring mvc config

    
        
            
        
    
    

    In @ExceptionHandler method

    @ExceptionHandler(Exception.class)
    public ResponseEntity handleException(Exception e, HttpServletRequest httpRequest) {
    
        RestError error = new RestError();
        error.setErrorCode(ErrorCodes.UNKNOWN_ERROR.getErrorCode());
        error.setDescription(ErrorCodes.UNKNOWN_ERROR.getDescription());
        error.setDescription(e.getMessage());
    
    
        logRestException(httpRequest, e);
    
        ResponseEntity responseEntity = new ResponseEntity(error,HttpStatus.INTERNAL_SERVER_ERROR);
        return responseEntity;
    }
    
    private void logRestException(HttpServletRequest request, Exception ex) {
        StringWriter sb = new StringWriter();
        sb.append("Rest Error \n");
        sb.append("\nRequest Path");
        sb.append("\n----------------------------------------------------------------\n");
        sb.append(request.getRequestURL());
        sb.append("\n----------------------------------------------------------------\n");
    Object requestBody = request.getAttribute(CustomJsonHttpMessageConverter.REQUEST_BODY_ATTRIBUTE_NAME);
    
        if(requestBody != null) { 
            sb.append("\nRequest Body\n");
            sb.append("----------------------------------------------------------------\n");
            sb.append(requestBody.toString());
    
            sb.append("\n----------------------------------------------------------------\n");
        }
    
        LOG.error(sb.toString());
    }
    

    I hope it helps :)

提交回复
热议问题