更正外部服务中重复文档的 HTTP 状态

Correct HTTP status for a duplicated document in external service

我正在为继承的服务开发 api 网关服务,客户向网关发送支付订单,网关将订单发送给遗留服务。每个付款订单都带有一个跟踪密钥,遗留服务使用此密钥检查订单是否重复。如果订单重复,遗留服务会向我发送状态 200,我需要在响应中查找错误 属性,但客户端可以更改跟踪键并重试,所以我正在考虑更改在网关中响应该错误并没有成功响应,但我不确定这是否是正确的方法以及向客户端抛出什么错误,一开始我在考虑 BadGatewayException,因为错误在遗留服务。

我认为 412 会更好,因为它表示客户端错误,而不是 502 Bad Gateway,后者表示接收端 system/server 错误

HTTP Status Codes

412 (Precondition Failed)
The 412 error response indicates that the client specified one or more preconditions in its request headers, effectively telling the REST API to carry out its request only if certain conditions were met. A 412 response indicates that those conditions were not met, so instead of carrying out the request, the API sends this status code.

我认为 409 的 Http 状态代码适合这种情况,因为您正在尝试创建服务器上已存在的订单。由于用户更改跟踪 ID 解决了冲突,我会说 return 409.

请参阅 HTTP 状态代码 409 的文档

The request could not be completed due to a conflict with the current state of the resource. This code is only allowed in situations where it is expected that the user might be able to resolve the conflict and resubmit the request. The response body SHOULD include enough information for the user to recognize the source of the conflict. Ideally, the response entity would include enough information for the user or user agent to fix the problem; however, that might not be possible and is not required.Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the entity being PUT included changes to a resource which conflict with those made by an earlier (third-party) request, the server might use the 409 response to indicate that it can't complete the request. In this case, the response entity would likely contain a list of the differences between the two versions in a format defined by the response Content-Type.

参考:HTTP Status Code Definitions