Swagger-ui在文件上传时报错net::ERR_CONNECTION_RESET
项目版本
spring-boot 2.1.6.RELEASE
swagger 2.7.0
在使用swagger做接口测试的时候遇到了一个诡异的问题,在文件超过限制大小的时候,没有按照全局异常拦截返回相应的错误码,而是直接抛出了一个net::ERR_CONNECTION_RESET的报错,页面没有任何返回显示
测试代码如下
controller
@PostMapping
@ApiOperation(value = "/upload", notes = "upload", httpMethod = "POST")
public ResultVO<Void> upload(@RequestPart(name = "file") MultipartFile file) throws Exception {
saveFile(FILE_SAVE_PATH, file.getOriginalFilename(), file);
return ResultVO.success();
}
全局异常拦截配置
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ResultVO<Void> exceptionHandler(Exception e) {
log.info("go to global exception handler: {}", e);
return ResultVO.error(ResultStateEnum.ERROR);
}
}
配置文件
server:
port: 10001
spring:
application:
name: spring-cloud-upload-swagger-demo
servlet:
multipart:
max-file-size: 30MB
max-request-size: 30MB
运行项目,打开swagger页面
上传一个小于30MB的文件,得到成功的返回
查看后台文件大小
INFO 1016 --- [io-10001-exec-1] c.m.d.u.s.controller.DemoController : file:springcloudgateway.jpg, size:46343
再上传超过30MB的文件,则没有任何返回显示
查看控制台,显示报错信息
查看后台打印,确实进入了全局异常
INFO 1016 --- [io-10001-exec-3] c.m.d.u.s.config.GlobalExceptionHandler : go to global exception handler: {}
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 (39356221) exceeds the configured maximum (31457280)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure