简体   繁体   English

REST Web服务-错误代码

[英]Rest Web Service - Error Codes

thanks for reading and attentions. 感谢您的阅读和关注。

My question is, exist or can I follow a table of HTTP codes to handle my error using REST Webservices ? 我的问题是,是否存在,或者我可以按照HTTP代码表使用REST Webservices处理错误吗? Let me explain better: 让我更好地解释一下:

UserRestWS (example) UserRestWS (示例)

  • "/users" GET , if error or not return 200, because return the empty or not empty list of users); “ / users” GET,如果错误或不返回200,因为返回用户列表为空或不为空);

  • "/{code}/user" GET, if find user return 200 with user as entity, otherwise 404 with custom error message. “ / {code} / user” GET,如果找到用户,则返回200,其中用户作为实体,否则返回404,显示自定义错误消息。

  • "/user/new" POST, if save operation ends successfully I return 200, otherwise for validation errors (some fields are wrong) ? “ / user / new” POST,如果保存操作成功结束,我返回200,否则返回验证错误(某些字段错误)? Server problem or computation error ? 服务器问题或计算错误?

  • "/{code}/update" PUT, same of above “ / {code} / update” PUT,同上

  • "/{code}/delete" DELETE, same of above “ / {code} / delete” DELETE,同上

I would follow a "standard" usage like HTTP codes, or other standard technique. 我会遵循“标准”用法,例如HTTP代码或其他标准技术。 Can you suggest me a table for this kind of problem ? 您能为我推荐一张解决此类问题的表格吗?

For me, it always is an 500 error code. 对我来说,它始终是500错误代码。

I googled and I find these links, not helpful for me. 我用Google搜索,发现这些链接对我没有帮助。 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error http://www.restapitutorial.com/httpstatuscodes.html https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error http://www.restapitutorial.com/httpstatuscodes.html

在此处输入图片说明

Http codes mapping: Http代码映射:

Creation – POST: 创建-POST:

•   200 – success : With either user details in response / user id in response
•   204 – No content : Indicates success – with no output
•   400 – Client input error : Whatever may be error with respect to input, 
•   500 – ISE: This is mapped as generic, for all other errors.

The tweak here is you can add the error code, so as all the cases can be covered while debugging the application. 这里的调整是您可以添加错误代码,以便在调试应用程序时可以解决所有情况。

For instance: 例如:

HTTP Error code: 500.
Internal Error code: APP-USER-1001 
Description:  Failed to create user with long user name. Name cannot cross 100 chars. 

Get all users: 获取所有用户:

•   200  - success : With all the users data
•   204 – no content : No users exist in the system
•   500 – ISE: Error while retrieving the users.

If every error is mapped to 500, doesn't make sense to API consumers to handle the situation. 如果每个错误都映射到500,则API使用者无法处理这种情况。

Similarly the API can be applied to rest of endpoints based on cases. 同样,可以根据情况将API应用于其他端点。

Here's the complete description. 这是完整的描述。

+---------+-----------+------------------------------------------------------------------------+
| Sr. No. | HTTP Code |  Description                                                           |
+---------+-----------+------------------------------------------------------------------------+
|       1 |       200 | OK, shows success.                                                     |
|       2 |       201 | CREATED, when a resource is successful created using POST or PUT       |
|         |           | request. Return link to newly created resource using location          |
|         |           | header.                                                                |
|       3 |       204 | NO CONTENT, when response body is empty for example, a DELETE          |
|         |           | request.                                                               |
|       4 |       304 | NOT MODIFIED, used to reduce network bandwidth usage in case of        |
|         |           | conditional GET requests. Response body should be empty. Headers       |
|         |           | should have date, location etc.                                        |
|       5 |       400 | BAD REQUEST, states that invalid input is provided e.g.                |
|         |           | validation error, missing data.                                        |
|       6 |       401 | UNAUTHORIZED, states that user is using invalid or wrong               |
|         |           | authentication token.                                                  |
|       7 |       403 | FORBIDDEN, states that user is not having access to method being       |
|         |           | used for example, delete access without admin rights.                  |
|       8 |       404 | NOT FOUND, states that method is not available.                        |
|       9 |       409 | CONFLICT, states conflict situation while executing the method         |
|         |           | for example, adding duplicate entry.                                   |
|      10 |       500 | INTERNAL SERVER ERROR, states that server has thrown some              |
|         |           | exception while executing the method.                                  |
+---------+-----------+------------------------------------------------------------------------+

References: http://www.tutorialspoint.com/restful/restful_quick_guide.htm 参考: http : //www.tutorialspoint.com/restful/restful_quick_guide.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM