【问题标题】:Controller Advice not handling FileTooLargeException by exceuting method annotated with FileTooLargeException控制器建议不通过执行使用 FileTooLargeException 注释的方法来处理 FileTooLargeException
【发布时间】:2020-08-16 19:42:24
【问题描述】:

我的休息控制器包含以下后期映射:

@PostMapping
    public ResponseEntity<RespDTO> uploadDocument(@ModelAttribute  @Valid RequestDTO requestDTO,@RequestParam(value = "fileContent") MultipartFile fileContent) throws  ServiceException, URISyntaxException { }

ServiceExceptionn 是特定于我的应用程序的自定义异常。

控制器建议如下所示:

@ControllerAdvice
public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait {

    @Override
    public ResponseEntity process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {

     }

    @ExceptionHandler(FileTooLargeException.class)
    public ResponseEntity<ResponseDTO> handleFileTooLargeException(FileTooLargeException ex, @Nonnull NativeWebRequest request){
      }
}

application.yml 包含以下属性:

spring:
  servlet:
    multipart:
      max-file-size: 2MB

如果我使用大小大于 2MB 的文件调用其余 api,那么我会遇到以下异常:

 io.undertow.server.handlers.form.MultiPartParserDefinition$FileTooLargeException: UT000054: The maximum size 5242880 for an individual file in a multipart request was exceeded

我在这里面临的问题是: 控制器建议未按预期工作。 handleFileTooLargeException - 这个方法必须被执行,因为它被注释了 ExceptionHandler 提到的特定异常类型。 但取而代之的是,控制进入控制器通知的处理方法。

无法理解我在这里缺少什么。

【问题讨论】:

标签: spring-boot controller-advice


【解决方案1】:

您需要ExceptionTranslator 类来扩展ResponseEntityExceptionHandler 类以启用带有@ExceptionHandler 注释的方法

【讨论】:

    猜你喜欢
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多