且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

spring rest 处理空请求体(400 Bad Request)

更新时间:2023-01-03 18:42:04

我解决了这个问题(自定义异常处理程序必须扩展 ResponseEntityExceptionHandler).我的解决方案如下:

I solved the issue (the custom exception handler must extend ResponseEntityExceptionHandler). My solution follows:

        @ControllerAdvice
        public class RestExceptionHandler extends ResponseEntityExceptionHandler {
    
            @Override
            protected ResponseEntity<Object> handleHttpMessageNotReadable(
                HttpMessageNotReadableException ex, HttpHeaders headers,
                HttpStatus status, WebRequest request) {
                // paste custom hadling here
            }
        }