首页 > 解决方案 > 春季不同的响应模型

问题描述

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

}

标签: javaspringerror-handlingswagger-codegen

解决方案


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


推荐阅读