我没有找到如何描述所有响应代码的方法,我只看到默认的响应代码。我有很多回应,想描述一下。此外,我有兴趣描述导致400响应错误的不同请求(例如,具有此类数据的请求将返回该消息等),是否应在API文档中进行描述?
如何在Apigility中自定义错误响应
Apigility与ZF Api Problem无缝配合,用于错误处理。
从控制器或侦听器创建错误响应非常简单:
use ZF'ApiProblem'ApiProblem;
...
return new ApiProblem(500, "My Internal Server Error");
常见错误的状态标题在类中的此处设置。
建议使用适合出现问题的有效http错误代码,但您当然可以自定义标题。您可以直接从控制器和侦听器返回ApiProblem
,Apigility
将正确处理error
,并返回具有设置为application/problem+json
的Content-Type
标头的渲染json
响应。
不确定这是否仍然存在,但您可以使用Api响应对象中的构造函数参数additional
来自定义您的答案;
return new ApiProblem(
Response::STATUS_CODE_500,
'Internal Server Error',
null,
'This is the title',
[
'specific' => 'entry',
'more_problem_details' => [
'custom_code' => 'custom_code_value',
'custom_message' => 'custom_message_text',
],
]
);
然后的反应会是这样的:
{
"specific": "entry",
"more_problem_details": {
"custom_code": "custom_code_value",
"custom_message": "custom_message_text"
},
"type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
"title": "This is the title",
"status": 500,
"detail": "Internal Server Error"
}
查看此项了解更多详细信息:
https://docs.zendframework.com/zend-problem-details/response/