MaxUploadSizeExceededException doesn't invoke the exception handling method in Spring

前端 未结 7 1997
不知归路
不知归路 2020-12-09 19:29

I\'m using Spring 3.2.0. According to this answer, I have the same method in my annotated controller which implements the HandlerExceptionResolver interface suc

7条回答
  •  渐次进展
    2020-12-09 19:42

    In your ControllerAdvice which is handling the Exception you can have code like this.It worked for me.This is in spring 4.0+

    @ExceptionHandler(Exception.class)
    public @ResponseBody BaseResponse onException(Exception e, HttpServletResponse response) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    
        BaseResponse resp = new BaseResponse();
        if(e instanceof MaxUploadSizeExceededException){
            resp.setCode(FileUploadFailed.SIZE_EXCEED);
            resp.setMessage("Maximum upload size exceeded");
        }
        return resp;
    }
    

提交回复
热议问题