【问题标题】:The ResponseEntity returned by @ExceptionHandler in @ControllerAdvice can't be shown in swagger-ui.html@ControllerAdvice中@ExceptionHandler返回的ResponseEntity不能在swagger-ui.html中显示
【发布时间】:2019-06-11 08:36:47
【问题描述】:

@ControllerAdvice 中 MaxUploadSizeExceededException 的 @ExceptionHandler 返回的 Response 无法在 swagger-ui.html 中显示。

我正在为我的 RESTful 应用程序使用 Spring Boot。并使用 swagger-ui 来使用 RESTful API。我的 RESTful API 之一是使用 Spring 的 MultipartFile 上传文件。

文件大小有限制,当上传的文件大小超过限制时,会抛出如下异常。 [请求处理失败;嵌套异常是 org.springframework.web.multipart.MaxUploadSizeExceededException: 超出最大上传大小...

为了处理此异常以向客户端返回自定义的错误消息,我使用了@ControllerAdvice 中的@ExceptionHandler 来返回响应。下面是我的代码。

    @ControllerAdvice
    public class GlobalControllerExceptionHandler{
        @ExceptionHandler(MaxUploadSizeExceededException.class)
        @ResponseBody
        public ResponseEntity<?> maxUploadSizeExceededException(MaxUploadSizeExceededException e) {        
            String bodyJson = "{\"ErrorMessage\":\"Maximum upload size exceeded.\"}";
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.TEXT_PLAIN).body(bodyJson);
        }
    }

我想在swagger-ui.html中看到的是这样的:

    Response Body:
    {"ErrorMessage":"Maximum upload size exceeded."}

    Response Code:
    500

但是现在,我没有看到任何内容:

    Response Body:
    no content

    Response Code:
    0

我在浏览器控制台上看到的一个异常症状是,请求头

临时标题显示“显示临时标题”:

!Provisional headers are shown
Accept: */*
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryf7WjEeoDHtIm3ly1
Origin: http://localhost:8080
Referer: http://localhost:8080/SpringApp/swagger-ui.html
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36
X-Requested-With: XMLHttpRequest

我搜索了“显示临时标头”,有人解释说,如果显示此类消息,则表示该请求实际上未发送。但就我而言,我实际上在 Spring Application 服务器端收到了请求,我什至可以点击 @ErrorHandler 方法“maxUploadSizeExceededException”。

【问题讨论】:

    标签: java spring-boot curl error-handling swagger-ui


    【解决方案1】:

    Swagger 不会解析您的 Java 实现。

    如果你想在文档中添加更多信息,你必须使用 Swagger 注释:

    https://github.com/swagger-api/swagger-core/wiki/Annotations-1.5.X

    在你的情况下是这样的:

    @ApiResponses(value = { 
          @ApiResponse(code = 500, message = "Maximum upload size exceeded.")
    })
    

    但是您必须将此注释添加到您的 RestController。 swagger 不考虑 ControllerAdvice。

    【讨论】:

    • 它不适用于我的情况。添加后症状没有变化:@ApiResponses(value = { @ApiResponse(code = 500, message = "xxx") })
    • 哦,现在我明白了。这是在控制建议中。大摇大摆地不考虑控制器建议。您必须将 ApiResonse 添加到 RestController 方法。
    • MaxUploadSizeExceededException 无法被 RestController 捕获。我认为这可能是因为这个异常是从 RestController 中抛出的。所以我必须使用 ControllerAdvice。
    • 可以,但是 api 文档必须在控制器上
    【解决方案2】:

    我终于找到了原因。关键症状“!显示临时标头”通常意味着请求发送失败。但实际上我已经在 GlobalControllerExceptionHandler::maxUploadSizeExceededException 上遇到了断点。因此,在我的情况下,“!显示临时标头”的解释应该是,请求已发送,但由于 MaxUploadSizeExceededException 数据未完全发布(仅上传了部分数据)。在浏览器上,如果请求发布没有完成,响应将被吞掉。所以 swagger-ui 无法显示任何响应。 如果使用 curl 命令进行文件上传,我可以在输出中得到预期的响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-29
      • 2019-01-30
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多