我必须创建几个 Web 服务来验证给定字段的可能值。我正在考虑做这样的事情:
POST /entity/fieldName body { fieldValues }
如果参数无效,POST 将返回 400(错误请求),否则返回 422(无法处理的实体)。但是我不太喜欢 422 响应部分,因为它使请求总是返回错误。另一方面,由于我只进行验证并且这是一个 POST,所以我不想在服务器上实际创建新资源(即返回 200)。是否有其他更适合此的 HTTP 方法/API 端点?对于它的值(value),我将检查实体字段 <fieldName>
其值在给定范围内。
最佳答案
如果您所做的只是验证,那么我认为您应该通过验证错误发送 422
并通过验证成功发送 200
。 POST 并不意味着您必须始终创建一个新实体。
The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.
If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).
关于java - 用于字段验证的 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39610331/