如何处理Spring Boot中的最大文件大小异常? [英] How to handle maximum file size Exception in Spring Boot?
问题描述
我使用Spring Boot v1.2.5创建REST应用程序。上传图片时,我检查了最大文件大小,提供了属性:
I am using Spring Boot v1.2.5 for creating a REST Application. While uploading images, I have a check for maximum file size , which is provided the property :
multipart.maxFileSize= 128KB
。该工具由Spring Boot本身提供。现在检查工作正常。问题是,如何处理异常并向用户返回他能理解的消息?
in application.properties. This facility is provided by Spring Boot itself. Now the check is working properly. The question is, how do I handle the exception and return a message to the user that he can understand ?
更新1 ----------
Update 1----------
我在Controller中编写了一个方法,我打算使用 @ExceptionHandler
来处理MultipartException。它似乎不起作用。
I wrote a method within my Controller, where I intend to handle the MultipartException, using @ExceptionHandler
. It does not seem to work.
这是我的代码:
@ExceptionHandler(MultipartException.class)
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
public ApplicationErrorDto handleMultipartException(MultipartException exception){
ApplicationErrorDto applicationErrorDto = new ApplicationErrorDto();
applicationErrorDto.setMessage("File size exceeded");
LOGGER.error("File size exceeded",exception);
return applicationErrorDto;
}
更新2 ----------
Update 2----------
@luboskrnac指出后,我设法找到了解决方案。我们可以在这里使用 ResponseEntityExceptionHandler
来处理这种特殊情况。我相信,我们也可以使用 DefaultHandlerExceptionResolver
,但 ResponseEntityExceptionHandler
将允许我们返回 ResponseEntity
,与前者相反,其方法将返回 ModelAndView
。我还没有试过它。
After @luboskrnac pointed it out, I have managed to come up with a solution. We can use ResponseEntityExceptionHandler
here to handle this particular case. I believe, we could also have used DefaultHandlerExceptionResolver
, but ResponseEntityExceptionHandler
will allow us to return a ResponseEntity
, as opposed to the former, the methods of which will return ModelAndView
. I have not tried it though.
这是我用来处理 MultipartException <的最终代码/ code>:
This is the final code that I'm using to handle the MultipartException
:
@ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
private static final Logger LOGGER = Logger.getLogger(CustomResponseEntityExceptionHandler.class);
@ExceptionHandler(MultipartException.class)
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
@ResponseBody
public ApplicationErrorDto handleMultipartException(MultipartException exception){
ApplicationErrorDto applicationErrorDto = new ApplicationErrorDto();
applicationErrorDto.setMessage("File size exceeded");
LOGGER.error("File size exceeded",exception);
return applicationErrorDto;
}
}
我使用Swagger开发/记录REST Apis。这是上传超过大小的文件时的响应。
谢谢。
I am using Swagger for developing/documenting REST Apis. This is the response upon uploading a file that exceeds the size. Thanks.
推荐答案
您还可以使用常规的Spring MVC功能,例如
@ExceptionHandler
方法和@ControllerAdvice
。然后ErrorController
将获得
任何未处理的异常。
You can also use regular Spring MVC features like
@ExceptionHandler
methods and@ControllerAdvice
. TheErrorController
will then pick up any unhandled exceptions.
由于 MultipartException
似乎在 @ Controller / @ ExceptionHandler / @ ControllerAdvice
功能开始之前出现,你应该使用 ErrorController
来处理它。
As MultipartException
seem to hapen before @Controller/@ExceptionHandler/@ControllerAdvice
features comes into play, you should use ErrorController
to handle it.
BTW,我发现这是在此期间的线程。你可能想看看。
BTW, I found this thread in the meantime. you may want to take a look.
这篇关于如何处理Spring Boot中的最大文件大小异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!