简体   繁体   English

如何使用 Spring 验证对大对象的 PATCH 请求

[英]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.

相关问题 验证补丁请求正文 - 弹出启动 - Validate patch request body -Spring boot 在Spring Boot中使用JsonPatchHandler处理PATCH请求 - Handling PATCH requests with JsonPatchHandler in Spring Boot 具有Spring Data Rest功能的自定义Spring MVC HTTP补丁请求 - Custom Spring MVC HTTP Patch requests with Spring Data Rest functionality 如何使用Spring在POST请求上将对象保留在会话范围内? - How do I persist objects in the session scope on POST requests with Spring? Spring Boot - ControllerAdvice exceptionhandler没有使用put和patch请求触发 - Spring Boot - ControllerAdvice exceptionhandler not firing with put and patch requests 如何按条件验证@RequestMapping请求? - How to validate @RequestMapping requests by condition? Spring @RestController 调用大量请求 - Spring @RestController invoking large number of requests 在Spring中的请求之间存储(可能很大)文件 - Storing a (possibly large) file between requests in Spring Spring Web:如何在提交表单时使用Spring Validator自动验证表单对象 - Spring web: how to validate form objects with Spring validator automatically at form submission 如果XSD的XSD出现唯一的粒子归因错误,如何在Spring Web Services中验证SPML SOAP请求? - How can i validate SPML SOAP Requests in Spring Web Services if their XSDs have Unique Particle Attribution Errors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM