[英]How to validate PATCH requests on large objects with Spring
I can't seem to find a lot of information about this.我似乎无法找到很多关于此的信息。 I've read that
PUT
requests should be used for replacing an entire resource with an updated resource, and that PATCH
requests should be used to update only the fields you want to change.我读过
PUT
请求应该用于用更新的资源替换整个资源,并且PATCH
请求应该用于仅更新您想要更改的字段。 But let's say I have a very large object with lots of nested fields and objects, and I want to update only specific fields in one of its nested objects rather than having to resubmit the entire thing (because it's just too large).但是,假设我有一个非常大的对象,其中包含许多嵌套字段和对象,我只想更新其中一个嵌套对象中的特定字段,而不必重新提交整个对象(因为它太大了)。 I would have to create a
PATCH
endpoint.我将不得不创建一个
PATCH
端点。 How would I go about validating such an endpoint in a Spring REST API?我将如何在 Spring REST API 中验证这样的端点? I can't get my head around how the endpoint would know which fields or nested fields it has received and how to validate them, and have it work for any possible field I could give it.
我无法理解端点如何知道它收到了哪些字段或嵌套字段以及如何验证它们,并让它适用于我可以提供的任何可能的字段。
When a request comes into spring, it will cast it to an object for example.例如,当一个请求进入 spring 时,它会将其转换为一个对象。
@PatchMapping("/customer/1")
public void updateCustomer(@RequestBody CustomerPatch updateRequest) {
}
Here we are casting the customer patch request to a CustomerPatch
object.在这里,我们将客户补丁请求转换为
CustomerPatch
对象。
With this, we can validate the object just as we would with any other object.有了这个,我们可以像验证任何其他对象一样验证对象。
I personally use the @Valid
annotation paired with hibernate validator.我个人使用与休眠验证器配对的
@Valid
注释。
so my spring controller would look like.所以我的弹簧控制器看起来像。
@PatchMapping("/customer/1")
public void updateCustomer(@RequestBody @Valid CustomerPatch updateRequest) {
}
public class CustomerPatch{
@NotNull
String cusotmerName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.