【问题标题】:Rest Web Service - Error CodesRest Web 服务 - 错误代码
【发布时间】:2016-05-31 13:06:05
【问题描述】:

感谢阅读和关注。

我的问题是,是否存在或者我可以按照 HTTP 代码表使用 REST Webservices 来处理我的错误?让我解释得更好:

UserRestWS(示例)

  • "/users" GET ,如果错误或不返回 200,因为返回空 或不是空的用户列表);

  • "/{code}/user" GET,如果找到用户返回200,用户为实体, 否则 404 并带有自定义错误消息。

  • "/user/new" POST,如果保存操作成功结束,我返回 200, 否则对于验证错误(某些字段是错误的)?服务器 问题还是计算错误?

  • "/{code}/update" PUT,同上

  • "/{code}/delete" 删除,同 以上

我会遵循“标准”用法,例如 HTTP 代码或其他标准技术。你能给我推荐一个解决这类问题的表格吗?

对我来说,它总是一个 500 错误代码。

我用谷歌搜索,发现这些链接对我没有帮助。 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error http://www.restapitutorial.com/httpstatuscodes.html

【问题讨论】:

    标签: java rest restful-architecture


    【解决方案1】:

    Http代码映射:

    创作 - 发布:

    •   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.
    

    这里的调整是您可以添加错误代码,以便在调试应用程序时涵盖所有情况。

    例如:

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

    获取所有用户:

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

    如果每个错误都映射到 500,那么 API 使用者处理这种情况就没有意义了。

    同样,API 可以根据案例应用于其他端点。

    【讨论】:

      【解决方案2】:

      这是完整的描述。

      +---------+-----------+------------------------------------------------------------------------+
      | 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.                                  |
      +---------+-----------+------------------------------------------------------------------------+
      

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

      【讨论】:

        猜你喜欢
        • 2020-12-16
        • 2014-12-14
        • 1970-01-01
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多