SpringBoot/Kotlin:多部分MaxUploadSizeExceedeException处理程序未启动

SpringBoot/Kotlin:多部分MaxUploadSizeExceedeException处理程序未启动,spring,kotlin,multipart,exceptionhandler,Spring,Kotlin,Multipart,Exceptionhandler,请原谅任何可怕的错误,我真的拿起科特林完全和一些春天大约一周前,由于一个项目的要求。我试图创建一个简单的RESTful API,其端点可以通过多部分接受文件。稍后,API之外会有一些页面显示HTML,我正在使用这些页面。据我所见,基于Java教程,异常处理应该是这样工作的 但是,对于API,我为MaxUploadSizeExceedeException定义的异常处理程序根本不会启动。My application.kt: @SpringBootApplication @EnableConfigur

请原谅任何可怕的错误,我真的拿起科特林完全和一些春天大约一周前,由于一个项目的要求。我试图创建一个简单的RESTful API,其端点可以通过多部分接受文件。稍后,API之外会有一些页面显示HTML,我正在使用这些页面。据我所见,基于Java教程,异常处理应该是这样工作的

但是,对于API,我为MaxUploadSizeExceedeException定义的异常处理程序根本不会启动。My application.kt:

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties::class)
class JaApplication {

    @Bean fun koreanderViewResolver(): ViewResolver = KoreanderViewResolver()
}

fun main(args: Array<String>) {
    runApplication<JaApplication>(*args)
}
当我试图通过curl发布一个大文件时,我得到一个JSON回复:

curl -F 'file=@path-to-large-file' http://localhost:8080/api/convert

{"timestamp":"2018-11-27T15:47:31.907+0000","status":500,"error":"Internal Serve
r Error","message":"Maximum upload size exceeded; nested exception is java.lang.
IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$Siz
eLimitExceededException: the request was rejected because its size (4294967496)
exceeds the configured maximum (529530880)","path":"/api/convert"}
Tomcat是否可能不将此异常传递给Spring?如果是的话,我怎样才能抓住这个?如果我可以将大小设置为无限,并在上载时检查文件大小,那么它也可以工作,尽管我需要在服务器开始接收文件之前这样做,并且我假设在到达/api/convert端点时,已经太晚了


谢谢您的帮助。

找到了问题。我为将来可能有同样问题的其他人发布

@ControllerAdvice
class ExceptionHandlers():ResponseEntityExceptionHandler() {

    @ExceptionHandler(MaxUploadSizeExceededException::class)
    fun handleException(e: MultipartException, request:WebRequest): ResponseEntity<Any> {
        return handleExceptionInternal(e, Result(message = "File is too large."), HttpHeaders(), HttpStatus.PAYLOAD_TOO_LARGE, request)
    }
}
@ControllerAdvice
class ExceptionHandlers():ResponseEntityExceptionHandler() {

    @ExceptionHandler(MaxUploadSizeExceededException::class)
    fun handleException(e: MultipartException, request:WebRequest): ResponseEntity<Any> {
        return handleExceptionInternal(e, Result(message = "File is too large."), HttpHeaders(), HttpStatus.PAYLOAD_TOO_LARGE, request)
    }
}