By default, when we have a repository with an open save method, we can execute the PATCH request. Then, Spring Data REST fetches the source object from the database and applies the changes to the entity, and then saves it for us (inside the JsonPatchHandler class). This allows us to execute the following query for the class
class Address { Long id; String street; Long houseNumber; }
PATCH / api / addresses / 1 with body
{ houseNumber: 123 }
And only this one field will be changed.
Now that we have a custom controller, we would like the whole object to be retrieved in the update method (after HATEOAS combined it with the original object from the database)
@RepositoryRestController @ExposesResourceFor(Address.class) @ResponseBody @RequestMapping("/addresses") public class AdddressController { @PatchMapping("/{addressId}") public Resource<Address> update(@RequestBody Resource<Address> addressResource, @PathVariable Long addressId) { Address address= addressResource.getContent();
Unfortunately, in the place where I would do some logic, I get an address with zero fields instead of a merged object.
Is it possible to connect a user controller in the Spring Data REST data stack so that when a request is corrected, it combines it for me (as well as for regular repositories)?
edit : I would like to find a solution that works transparently with both PATCH (content-type: application / json-patch + json) and PATCH (content-type: application / hal + json)
Paweł source share