我正在使用swagger-codegen-3.0.0。
如下所示,API规范的响应为200和400;但是,当生成addTeam()API时,会以返回类型'void'生成它。
我想处理响应代码200和/或400。这是否意味着我已经在响应规范中明确定义了有效负载类型?有人可以提供更多有关“响应”规范的详细信息吗?
49 /team:
50 post:
51 summary: Add team
52 operationId: addTeam
53 requestBody:
54 description: Team detail being added
55 content:
56 application/json:
57 schema:
58 type: array
59 items:
60 $ref: "#/components/schemas/addTeamPayload"
61 responses:
62 200:
63 description: Ok
64 400:
65 description: Bad request
66 tags:
67 - Team
java -jar swagger-codegen-cli-3.0.0.jar生成-i teamApiSpec.yaml -l java --additional-properties jackson = true --artifact-id swagger-java-client-api
此命令生成以下Java代码/ API。
/**
* Add team
*
* @param body Team detail being added (optional)
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
*/
public void addTeam(List<AddTeamPayload> body) throws ApiException {
addTeamWithHttpInfo(body);
}
/**
* Add Team
*
* @param body Team detail being added (optional)
* @return ApiResponse<Void>
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
*/
public ApiResponse<Void> addTeamWithHttpInfo(List<AddTeamPayload> body) throws ApiException {
com.squareup.okhttp.Call call = addTeamValidateBeforeCall(body, null, null);
return apiClient.execute(call);
}
另一个问题是,即使在API规范中编写了400个响应代码,当服务器返回400时,API仍会引发异常,并且在处理过程中,返回代码的详细信息将丢失。作为API的用户,我不知道服务器返回了什么返回码或发送了什么返回响应消息。
有人可以对此发表评论吗?这个很重要。让我知道我是否错过了API规范中的内容。
答案 0 :(得分:0)
如果服务返回数据,则应在响应中添加内容描述。
/team:
post:
summary: Add team
requestBody:
description: Team detail being added
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/addTeamPayLoad'
responses:
200:
description: Ok
content:
'*/*':
schema:
type: object
400:
description: Bad request
content:
'*/*':
schema:
type: object
tags:
- Team