Tomcat:ExceptionHasndler不适用于MultiPartException,但对IllegalArgumentException工作正常

时间:2017-11-20 16:33:04

标签: java spring tomcat spring-boot exceptionhandler

我有这样的控制器:

@PostMapping("/rest_upload1")
public ResponseEntity upload1(@RequestParam("file") MultipartFile multipartFile) throws IOException {
  throw new IllegalArgumentException();
}

在配置中我有设置:

spring.http.multipart.max-file-size=100MB
spring.http.multipart.max-request-size=100MB

这意味着如果文件超过100MB,spring将抛出MultipartException

为了处理这个异常,我写了处理程序:

@ControllerAdvice
public class RestExceptionHandlerAdvice extends ResponseEntityExceptionHandler {

    @ExceptionHandler(MultipartException.class)
    @ResponseBody
    public ResponseEntity<ApiError> handleException(MultipartException e) {
         logger.warn("MultipartException:", e);
         ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST,
                String.valueOf(HttpStatus.BAD_REQUEST),
                ExceptionUtils.getRootCauseMessage(e),
                Collections.emptyList());
        return new ResponseEntity<ApiError>(apiError, HttpStatus.BAD_REQUEST);        
    }

如果出现错误,此代码将调用(我在调试中看到它)

但在浏览器中我看不到回复:

enter image description here

enter image description here

我用Google搜索了很多时间,并且看起来很可爱。我试图为IllegalArgumentException添加处理程序:

@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public ResponseEntity<ApiError> handleException(IllegalArgumentException e) {

    ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST,
                String.valueOf(HttpStatus.BAD_REQUEST),
                ExceptionUtils.getRootCauseMessage(e),
                Collections.emptyList());

    return new ResponseEntity<ApiError>(apiError, HttpStatus.BAD_REQUEST);            
}

我上传的文件少于100mb。在这种情况下结果不同:

enter image description here

但无论如何,响应代码是错误的。

有什么不对?

P.S。

我试过了:

@ExceptionHandler(MultipartException.class)
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
@ResponseBody
public String handleException(MultipartException e) {
    return ExceptionUtils.getRootCauseMessage(e);      
}

它看起来和这里一样:

How to handle maximum file size Exception in Spring Boot?

P.S.2

我找到了解决方法,但它看起来像sring中的bug:

我添加了依赖项:

compile "org.apache.commons:commons-io:1.3.2"
compile "commons-fileupload:commons-fileupload:1.3.3"

注册bean:

@Bean
public MultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
     multipartResolver.setMaxUploadSize(10);
     return multipartResolver;
}

并写道:

@ControllerAdvice
public class UploadExceptionHandler {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseStatus(HttpStatus.PAYLOAD_TOO_LARGE)
    @ResponseBody
    public String handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        return e.getMessage();
    }
}

1 个答案:

答案 0 :(得分:0)

不是很聪明的tomcat开发人员添加了我们需要禁用/覆盖的新功能。

我添加后它正在工作:

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {

        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

        tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {

            // connector other settings...

            // configure maxSwallowSize
            if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
                // -1 means unlimited, accept bytes
                ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }

        });

        return tomcat;

    }