我正在开发一个Spring Boot应用程序,其中有一个用于图库功能的图像文件上传。我想限制图像的文件大小,并在超出大小时在其余端点上返回错误消息。
我当前的设置如下。
我的控制器
@RestController
@RequestMapping("/api/gallery")
class GalleryController {
@PostMapping("/upload/{eventYear}/{eventIdentifier}")
public ImageUploadResponseDto uploadImage(@RequestParam("file") MultipartFile file, @PathVariable("eventYear") int year, @PathVariable("eventIdentifier") String identifier) {
// do something with the file
}
}
用于错误处理的控制器建议
@ControllerAdvice
class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
@ExceptionHandler({MaxUploadSizeExceededException.class})
public ValidationException handleSizeExceededException(MaxUploadSizeExceededException ex): ValidationException {
return ValidationKeys.FILE_SIZE_EXCEEDED.makeException(nestedCause = ex)
}
}
application.yml
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
除了可以正常工作外,您会发现其他错误,但当我向Postman提出请求时,会收到此错误。
但是当我查看spring应用程序的日志时,它说响应已按要求编写,并且已捕获超出文件大小限制的异常。
2018-08-18 20:30:53.180 TRACE 10520 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Found 1 matching mapping(s) for [/api/gallery/upload/2018/konzert_1] : [{[/api/gallery/upload/{eventYear}/{eventIdentifier}],methods=[POST]}]
2018-08-18 20:30:53.180 DEBUG 10520 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public ch.mginsmoerigen.server.web.model.ImageUploadResponseDto ch.mginsmoerigen.server.web.controller.GalleryController.uploadImage(org.springframework.web.multipart.MultipartFile,int,java.lang.String)]
2018-08-18 20:30:53.182 TRACE 10520 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Bound request context to thread: org.apache.catalina.connector.RequestFacade@5bf77c94
2018-08-18 20:30:53.182 DEBUG 10520 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/api/gallery/upload/2018/konzert_1]
2018-08-18 20:30:53.184 DEBUG 10520 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (14776581) exceeds the configured maximum (10485760)
2018-08-18 20:30:53.187 DEBUG 10520 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Invoking @ExceptionHandler method: public ch.mginsmoerigen.server.web.exception.ValidationException ch.mginsmoerigen.server.web.config.GlobalExceptionHandler.handleSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException)
2018-08-18 20:30:53.188 TRACE 10520 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking 'ch.mginsmoerigen.server.web.config.GlobalExceptionHandler.handleSizeExceededException' with arguments [org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (14776581) exceeds the configured maximum (10485760)]
2018-08-18 20:30:53.189 TRACE 10520 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [ch.mginsmoerigen.server.web.config.GlobalExceptionHandler.handleSizeExceededException] returned [ch.mginsmoerigen.server.web.exception.ValidationException: i18n.error.http.fileSizeExceeded.message]
2018-08-18 20:30:53.301 DEBUG 10520 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Written [ch.mginsmoerigen.server.web.exception.ValidationException: i18n.error.http.fileSizeExceeded.message] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@5feaada1]
2018-08-18 20:30:53.301 WARN 10520 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (14776581) exceeds the configured maximum (10485760)
2018-08-18 20:30:53.301 DEBUG 10520 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-08-18 20:30:53.301 TRACE 10520 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@5bf77c94
2018-08-18 20:30:53.302 DEBUG 10520 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
因此,根据我的应用程序,一切正常,响应主体已设置。
现在对我来说是最令人困惑的部分。 为了查看响应是否真的写得正确,我安装了提琴手,它可以拦截到Spring Boot应用程序的流量。
现在,当提琴手运行时,我的响应会传递给邮递员,并且在提琴手的检查器工具中也可以看到,但是一旦提琴手关闭,便无法再进行任何操作。
当提琴手追赶响应时会发生什么?
仅在Windows上出现问题。在我的Ubuntu VM中,它就像一个魅力。