java - 春季不同的响应模型
问题描述
我正在使用 maven codgen 插件为我生成代码。但是,如果捕获到 API 异常,我想返回一个不同的模型。但它似乎不起作用。
我想返回 e.getResponseBody(); 无论外部 api 返回什么。但错误模型与 List 不同。
例如。使用简单的宠物示例(并将“默认”响应更改为“400”):
responses:
'200':
description: pet response
schema:
type: array
items:
$ref: '#/definitions/pet'
'400':
description: unexpected error
schema:
$ref: '#/definitions/errorModel'
生成如下界面:
@ApiOperation(value = "", notes = "Returns all pets from the system that the user has access to", response = Pet.class, responseContainer = "List", tags={ })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "pet response", response = Pet.class),
@ApiResponse(code = 400, message = "unexpected error", response = errorModel.class) })
@RequestMapping(value = "/pets",
produces = { "application/json", "application/xml", "text/xml", "text/html" },
consumes = { "application/json" },
method = RequestMethod.GET)
ResponseEntity<List<Pet>> findPets(@ApiParam(value = "tags to filter by") @RequestParam(value = "tags", required = false) List<String> tags,
@ApiParam(value = "maximum number of results to return") @RequestParam(value = "limit", required = false) Integer limit);
控制器
ResponseEntity<List<Pet>> findPets(@ApiParam(value = "tags to filter by") @RequestParam(value = "tags", required = false) List<String> tags,
@ApiParam(value = "maximum number of results to return") @RequestParam(value = "limit", required = false) Integer limit){
try{
//call external api which throw a API exception if fail
}
catch(ApiException e){
// I would like to return e.getResponseBody(); whatever it is returned by external api.
// But the error model is not the same as List<Pet>
}
}
解决方案
Spring 为您提供了一种比ApiException
直接在控制器中捕获更好的方法。您应该记录自己在 REST apis 中的异常处理(考虑阅读https://www.baeldung.com/exception-handling-for-rest-with-spring)
根据您的 spring 版本,您可以使用@ControllerAdvice
注释,我认为这是最简单的方法。
从控制器中删除 try/catch :
ResponseEntity<List<Pet>> findPets(@ApiParam(value = "tags to filter by") @RequestParam(value = "tags", required = false) List<String> tags,
@ApiParam(value = "maximum number of results to return") @RequestParam(value = "limit", required = false) Integer limit)
throws ApiException
{
//call external api which throw a API exception if fail
}
创建一个带有注释的类@ControllerAdvice
@ControllerAdvice
public class APIExceptionHandler
extends ResponseEntityExceptionHandler {
@ExceptionHandler(value
= ApiException.class)
protected ResponseEntity<MyExceptionResponseDto> handleException(
ApiException ex, WebRequest request) {
MyExceptionResponseDto apiError = ... //TODO
return new ResponseEntity<>(apiError, HttpStatus.INTERNAL_SERVER_ERROR)
}
}
注意:MyExceptionResponseDto
是代表您的错误响应的 DTO
这还允许您选择一个特定的 HTTP 错误代码,该代码在客户端处理错误时很重要(不仅仅是基于 json 中的属性)
推荐阅读
- python - 使用 Python 写入 HTML 文件
- javascript - 在 VSCode 中如何正确配置 jsconfig.json 以使带有 index.js 导入的绝对路径起作用?
- r - dplyr 用第一行替换 colnames 并使用管道函数删除第一行
- dialog - wxPython:如何根据嵌入的StaticText的内容使Dialog调整大小
- c - C 中未初始化的局部变量中包含的值到底是什么?
- c++ - Boost Socket 在 close()
- wordpress - 将 wordpress 文件上传到虚拟主机
- ios - 键盘没有显示 becomeFirstResponder()
- sql - (T-SQL) 为什么这个子查询需要别名?
- javascript - 如何在 Firebase Cloud 函数中调用导出的 https Firebase Cloud 函数?