How to handle multiple response/return types (empty for 204, non-empty for 400 etc) in swagger codegen?(如何在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为非空等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在swagger codegen中处理多种响应/返回类型(204为空,400为非空等)?


基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01