@ControllerAdvice 中@ExceptionHandler 返回的ResponseEntity 无法显示在swagger-ui.html
The ResponseEntity returned by @ExceptionHandler in @ControllerAdvice can't be shown in swagger-ui.html
@ExceptionHandler 为 @ControllerAdvice 中的 MaxUploadSizeExceededException 编辑的 return 响应无法在 swagger-ui.html.
中显示
我正在为我的 RESTful 应用程序使用 Spring 启动。并使用 swagger-ui 来使用 RESTful API。我的 RESTful API 之一是使用 Spring 的 MultipartFile 上传文件。
文件大小有限制,当上传的文件大小超过限制时,会抛出如下异常。
[请求处理失败;嵌套异常是 org.springframework.web.multipart.MaxUploadSizeExceededException:超出最大上传大小...
为了将此异常处理为 return 给客户端的自定义错误消息,我使用 @ControllerAdvice 中的 @ExceptionHandler 来 return 响应。下面是我的代码。
@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
我在浏览器控制台上看到的一个异常症状是,它们请求 header
临时 headers 显示 "Provisional headers are shown":
!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
我搜了一下"Provisional headers are shown",有人解释说,如果出现这样的信息,说明实际上没有发送请求。但就我而言,我实际上在 Spring 应用程序服务器端收到了请求,我什至可以点击 @ErrorHandler 方法 "maxUploadSizeExceededException".
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 中。 ControllerAdvice 不被 swagger 考虑。
终于找到原因了。关键症状“!Provisional headers are shown”一般表示请求发送失败。但实际上我已经在 GlobalControllerExceptionHandler::maxUploadSizeExceededException 上设置了断点。因此,在我的案例中,对“!Provisional headers are shown”的解释应该是,请求已发送,但由于 MaxUploadSizeExceededException(仅上传了部分数据),数据未完全 posted。在浏览器上,如果请求 post 没有完成,响应将被吞噬。所以 swagger-ui 无法显示任何响应。
如果使用 curl 命令进行文件上传,我可以在输出中得到预期的响应。
@ExceptionHandler 为 @ControllerAdvice 中的 MaxUploadSizeExceededException 编辑的 return 响应无法在 swagger-ui.html.
中显示我正在为我的 RESTful 应用程序使用 Spring 启动。并使用 swagger-ui 来使用 RESTful API。我的 RESTful API 之一是使用 Spring 的 MultipartFile 上传文件。
文件大小有限制,当上传的文件大小超过限制时,会抛出如下异常。 [请求处理失败;嵌套异常是 org.springframework.web.multipart.MaxUploadSizeExceededException:超出最大上传大小...
为了将此异常处理为 return 给客户端的自定义错误消息,我使用 @ControllerAdvice 中的 @ExceptionHandler 来 return 响应。下面是我的代码。
@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
我在浏览器控制台上看到的一个异常症状是,它们请求 header
临时 headers 显示 "Provisional headers are shown":
!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
我搜了一下"Provisional headers are shown",有人解释说,如果出现这样的信息,说明实际上没有发送请求。但就我而言,我实际上在 Spring 应用程序服务器端收到了请求,我什至可以点击 @ErrorHandler 方法 "maxUploadSizeExceededException".
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 中。 ControllerAdvice 不被 swagger 考虑。
终于找到原因了。关键症状“!Provisional headers are shown”一般表示请求发送失败。但实际上我已经在 GlobalControllerExceptionHandler::maxUploadSizeExceededException 上设置了断点。因此,在我的案例中,对“!Provisional headers are shown”的解释应该是,请求已发送,但由于 MaxUploadSizeExceededException(仅上传了部分数据),数据未完全 posted。在浏览器上,如果请求 post 没有完成,响应将被吞噬。所以 swagger-ui 无法显示任何响应。 如果使用 curl 命令进行文件上传,我可以在输出中得到预期的响应。