如何在swagger codegen中处理多种响应/返回类型(204为空,400为非空等)? [英] How to handle multiple response/return types (empty for 204, non-empty for 400 etc) in swagger codegen?

查看:63
本文介绍了如何在swagger codegen中处理多种响应/返回类型(204为空,400为非空等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Openapi 3.0.2版。

我有以下规范来描述我的响应:

responses:
    '201':
        description:  
            Created
    '400':
        description: Bad request
        content:
            application/json:
                schema:
                    $ref: '#/components/schemas/Error'
    '404':
        description: The resource could not be found.
    '500':
        description: The request failed due to an unexpected server error.
对于大多数响应代码,我不返回任何响应正文,但对于400响应 代码,我想返回错误对象:

Error:
    type: object
    properties:
        code:
            type: string
        message:
            type: string
    required:
        - code
        - message
当我为此终结点生成Java服务器代码时,方法的返回类型 是ResponseEntity<Void>,表示无法返回错误对象?

似乎类似于以下问题:

https://groups.google.com/forum/#!topic/swagger-swaggersocket/ygVjA2m5gY0

https://github.com/swagger-api/swagger-codegen/issues/7743

https://github.com/swagger-api/swagger-codegen/issues/4398

我不知道这是不是已经修复的问题,或者是否有解决此问题的方法?

推荐答案

我使用了下面介绍的方法here来解决这个问题:

@Override
public ResponseEntity<Void> deleteThing(...)
{
        try {
                myService.deleteThing(...);
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (MyException e) {
                    throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
        }
        catch (MyOtherException e) {
                    throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
        }
}

这篇关于如何在swagger codegen中处理多种响应/返回类型(204为空,400为非空等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆