java - 用于字段验证的 REST API

标签 java web-services rest validation

我必须创建几个 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/

相关文章:

java - 如何检查单击按钮时是否选择了单选按钮?

c# - 如果服务引用不起作用,我能做些什么吗?

rest - Firebase 身份验证 + 自己的 API

java - 如何从单个 Web 服务类调用不同的方法

java - Spring Boot 数据休息 Jakson JSONSerializer 空

java - 如何为2个参数实现单个DeleteMapping?

java - 正弦函数的结果在微软和 Java/Linux 之间不同

java - 在继承父 pom 的同时管理 Maven 多模块构建

java - 下载文件时在浏览器中提示加载

java - 调用 webservice 时绕过 HTTPS 证书验证