春季不同的响应模型

时间:2021-07-29 05:21:23

标签: java spring error-handling swagger-codegen

我正在使用 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>
}

}

1 个答案:

答案 0 :(得分:1)

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 中的属性)

相关问题