Swagger-ui在文件上传时报错net::ERR_CONNECTION_RESET

在使用Swagger-ui进行接口测试时,当上传文件超过一定大小,出现net::ERR_CONNECTION_RESET错误,而非预期的全局异常响应。问题出现在Spring Boot 2.1.6.RELEASE和Swagger 2.7.0环境下。解决方案包括调整配置文件中最大请求尺寸限制和添加文件上传拦截器。经测试,POSTMAN能正确返回全局异常,而更换Servlet容器如Jetty或Undertow无法解决问题。
摘要由CSDN通过智能技术生成

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值