We are trying to implement a special partial update function in Spring 3.2. We are using Spring for the backend and have a simple Javascript frontend. I\'ve not been able to
I've done this with a java Map and some reflection magic:
public static Entidade setFieldsByMap(Map dados, Entidade entidade) {
dados.entrySet().stream().
filter(e -> e.getValue() != null).
forEach(e -> {
try {
Method setter = entidade.getClass().
getMethod("set"+ Strings.capitalize(e.getKey()),
Class.forName(e.getValue().getClass().getTypeName()));
setter.invoke(entidade, e.getValue());
} catch (Exception ex) { // a lot of exceptions
throw new WebServiceRuntimeException("ws.reflection.error", ex);
}
});
return entidade;
}
And the entry point:
@Transactional
@PatchMapping("/{id}")
public ResponseEntity partialUpdate(@PathVariable String entity,
@PathVariable Long id, @RequestBody Map data) {
// ...
return new ResponseEntity<>(obj, HttpStatus.OK);
}