我正在使用SpringBoot和SpringBoot。我想了解更新模型属性的HTTP修补程序方法
有什么好的教程来解释如何使它工作吗?
发布于 2019-04-09 14:18:55
我注意到,许多提供的答案都是JSON补丁或不完整的答案。下面是一个完整的说明和示例,说明您需要使用实用的真实世界代码。
首先,补丁是一个选择性的放置。您可以使用它来更新对象或对象列表的任意数量的字段。在PUT中,通常使用任何更新发送整个对象。
补丁/对象/7
{
"objId":7,
"objName": "New name"
}放置/对象/7
{
"objId":7,
"objName": "New name",
"objectUpdates": true,
"objectStatus": "ongoing",
"scoring": null,
"objectChildren":[
{
"childId": 1
},
............
}这允许您在不需要大量端点的情况下更新记录。例如,要更新评分,需要object/{id}/scoring,然后要更新名称,则需要object/{id}/name。实际上,每个项都有一个端点,或者需要前端为每个更新发布整个对象。如果您有一个庞大的对象,这可能需要大量的网络时间或移动数据,这是不必要的。该修补程序允许您拥有一个具有最小对象属性的端点,移动平台应该使用这个端点。
下面是一个用于修补程序的真实世界的示例:
@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找到相关的声明,然后只更新作为键值对传入的字段。
示例体:
补丁/索赔/7
{
"claimId":7,
"claimTypeId": 1,
"claimStatus": null
}上面的内容将将claimTypeId和claimStatus更新为索赔7的给定值,而所有其他值都保持不变。
所以回报应该是:
{
"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
}如您所见,完整的对象将返回,而不会更改除要更改的数据之外的任何其他数据。我知道这里的解释有点重复,我只是想清楚地概述一下。
发布于 2015-05-01 15:08:12
就Spring而言,PATCH方法与PUT和POST没有本质上的区别。挑战在于您在修补程序请求中传递了什么,以及如何在Controller中映射数据。如果使用@RequestBody映射到值bean,则必须计算实际设置的内容和空值的含义。其他选项是将PATCH请求限制为一个属性,并在url中指定它,或者将值映射到Map。另见Spring MVC PATCH method: partial updates
发布于 2017-03-20 10:04:15
创建rest模板,使用-
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
RestTemplate rest = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
now make the PATCH call
ResponseEntity<Map<String, Object>> response = rest.exchange(api, HttpMethod.PATCH, request,
responseType);https://stackoverflow.com/questions/29988841
复制相似问题