Spring REST和PATCH方法 [英] Spring REST and PATCH method

查看:257
本文介绍了Spring REST和PATCH方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpringBoot和Spring REST. 我想了解HTTP PATCH方法来更新我的模型的属性

I'm using SpringBoot and Spring REST. I would like to understand the HTTP PATCH method to update properties of my Model

有没有很好的教程来说明如何使其工作?

Is there any good tutorial explaining how to make it works ?

  • 要发送的HTTP PATCH方法和正文
  • 控制器方法以及如何管理更新操作

推荐答案

我注意到提供的许多答案都是JSON修补程序或不完整的答案.以下是使用现实世界代码所需功能的完整说明和示例

I've noticed that many of the provided answers are all JSON patching or incomplete answers. Below is a full explanation and example of what you need with functioning real world code

首先,PATCH是选择性PUT.您可以使用它来更新对象或对象列表的任意数量的字段.在PUT中,通常会随更新一起发送整个对象.

First, PATCH is a selective PUT. You use it to update any number of fields for an object or list of objects. In a PUT you typically send the entire object with whatever updates.

PATCH/object/7

PATCH /object/7

{
   "objId":7,
   "objName": "New name"
}

PUT/object/7

PUT /object/7

{
   "objId":7,
   "objName": "New name",
   "objectUpdates": true,
   "objectStatus": "ongoing",
   "scoring": null,
   "objectChildren":[
       {
          "childId": 1
       },
     ............ 
}

这使您可以在没有大量端点的情况下更新记录.例如,在上面,要更新评分,您需要对象/{id}/评分,然后要更新名称,您需要对象/{id}/名称.从字面上看,每个项目都有一个端点,或者您需要前端为每个更新发布整个对象.如果您有一个巨大的物体,这可能会花费大量的网络时间或不必要的移动数据.该补丁使您可以为1个端点提供移动平台应使用的具有最少对象属性的发送.

This allows you to update records without huge amounts of endpoints. For example, with above, to update scoring you need object/{id}/scoring, then to update name you need object/{id}/name. Literally one endpoint for every item or you require the front end to post the entire object for every update. If you have a huge object, this can take a lot of network time or mobile data that is unnecessary. The patch lets you have 1 endpoint with the minimal object property sends that a mobile platform should use.

这是现实世界中补丁使用的示例:

here is an example of a real world use for patch:

@ApiOperation(value = "Patch an existing claim with partial update")
@RequestMapping(value = CLAIMS_V1 + "/{claimId}", method = RequestMethod.PATCH)
ResponseEntity<Claim> patchClaim(@PathVariable Long claimId, @RequestBody Map<String, Object> fields) {

    // Sanitize and validate the data
    if (claimId <= 0 || fields == null || fields.isEmpty() || !fields.get("claimId").equals(claimId)){
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST); // 400 Invalid claim object received or invalid id or id does not match object
    }

    Claim claim = claimService.get(claimId);

    // Does the object exist?
    if( claim == null){
        return new ResponseEntity<>(HttpStatus.NOT_FOUND); // 404 Claim object does not exist
    }

    // Remove id from request, we don't ever want to change the id.
    // This is not necessary, you can just do it to save time on the reflection
    // loop used below since we checked the id above
    fields.remove("claimId");

    fields.forEach((k, v) -> {
        // use reflection to get field k on object and set it to value v
        // Change Claim.class to whatver your object is: Object.class
        Field field = ReflectionUtils.findField(Claim.class, k); // find field in the object class
        field.setAccessible(true); 
        ReflectionUtils.setField(field, claim, v); // set given field for defined object to value V
    });

    claimService.saveOrUpdate(claim);
    return new ResponseEntity<>(claim, HttpStatus.OK);
}

以上内容可能会使某些人感到困惑,因为较新的开发人员通常不会这样处理反射.基本上,无论您在主体中传递此函数如何,它都会使用给定的ID查找关联的声明,然后仅更新作为键值对传递的字段.

The above can be confusing for some people as newer devs don't normally deal with reflection like that. Basically, whatever you pass this function in the body, it will find the associated claim using the given ID, then ONLY update the fields you pass in as a key value pair.

示例正文:

PATCH/claims/7

PATCH /claims/7

{
   "claimId":7,
   "claimTypeId": 1,
   "claimStatus": null
}

以上内容会将ClaimTypeId和ClaimStatus更新为声明7的给定值,而所有其他值均保持不变.

The above will update claimTypeId and claimStatus to the given values for claim 7, leaving all other values untouched.

所以回报会是这样的:

{
   "claimId": 7,
   "claimSrcAcctId": 12345678,
   "claimTypeId": 1,
   "claimDescription": "The vehicle is damaged beyond repair",
   "claimDateSubmitted": "2019-01-11 17:43:43",
   "claimStatus": null,
   "claimDateUpdated": "2019-04-09 13:43:07",
   "claimAcctAddress": "123 Sesame St, Charlotte, NC 28282",
   "claimContactName": "Steve Smith",
   "claimContactPhone": "777-555-1111",
   "claimContactEmail": "steve.smith@domain.com",
   "claimWitness": true,
   "claimWitnessFirstName": "Stan",
   "claimWitnessLastName": "Smith",
   "claimWitnessPhone": "777-777-7777",
   "claimDate": "2019-01-11 17:43:43",
   "claimDateEnd": "2019-01-11 12:43:43",
   "claimInvestigation": null,
   "scoring": null
}

如您所见,完整对象将返回,而不更改除您要更改的数据以外的任何数据.我知道这里的解释有些重复,我只想清楚地概述一下.

As you can see, the full object would come back without changing any data other than what you want to change. I know there is a bit of repetition in the explanation here, I just wanted to outline it clearly.

这篇关于Spring REST和PATCH方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆