【发布时间】:2021-07-29 05:21:23
【问题描述】:
我正在使用 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>
}
}
【问题讨论】:
标签: java spring error-handling swagger-codegen