Json SpringRESTTemplatePatch:将我的DTO映射到外部DTO的方法

Json SpringRESTTemplatePatch:将我的DTO映射到外部DTO的方法,json,rest,http,spring-mvc,Json,Rest,Http,Spring Mvc,我有一个补丁请求端点,用于部分更新我的应用程序中的人员: @PatchMapping("/person/{id}") public ResponseEntity<?> updatePerson(@PathVariable Long id, @RequestBody String patchJson) { service.updatePerson(id, patchJson); return ResponseEntity.noContent().bu

我有一个补丁请求端点,用于部分更新我的应用程序中的人员:

@PatchMapping("/person/{id}")
public ResponseEntity<?> updatePerson(@PathVariable Long id, @RequestBody String patchJson) {
    service.updatePerson(id, patchJson);
    return ResponseEntity.noContent().build();
}
但我还需要使用
Person
entity中的一些字段部分更新外部服务中的实体,只是在我的应用程序中部分更新

使用
restemplate
调用有一种方法:

public void patchExternal(Long extId, ExtServiceDto extServiceDto) {
    HttpEntity<ExtServiceDto> request = new HttpEntity<>(extServiceDto);
    restTemplate.exchange(extApiUrl, HttpMethod.PATCH, request, Some.class);
}
问题是:将patchJson映射到extServiceDto的最正确方法是什么


主要的困难是区分空字段和故意设置为空的字段。

通常我会使用以下方法手动映射:

  Optional.ofNullable(newItem.getName()).orElse(existingItem.getName()));
对于很多字段来说,这是很乏味的,但是如果您不想意外地将字段空掉,这是可行的

或者,可以用于从DTO复制字段,但空筛选更难

{
    ...
    "someDate": "2020-09-04",
    "someType": "MANAGER",
    ...
    "ext_api_fields": ...
}
  Optional.ofNullable(newItem.getName()).orElse(existingItem.getName()));