我使用的是 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对象:
Error:
type: object
properties:
code:
type: string
message:
type: string
required:
- code
- message
当我为此端点生成 Java 服务器代码时,该方法的返回类型
是 ResponseEntity<Void>
,这意味着我无法返回 Error 对象?
看起来与这些问题相似:
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());
}
}
关于java - 如何在 swagger codegen 中处理多种响应/返回类型(204 为空,400 非空等)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60325210/