Custom Spring MVC HTTP Patch requests with Spring Data Rest functionality
Asked Answered
W

1

5

What is the best practice for supporting HTTP PATCH in custom Spring MVC controllers? Particularly when using HATEOAS/HAL? Is there an easier way to merge objects without having to check for the presence of every single field in the request json (or writing and maintaining DTOs), ideally with automatic unmarshalling of links to resources?

I know this functionality exists in Spring Data Rest, but is it possible to leverage this for use in custom controllers?

Wellfound answered 22/10, 2015 at 19:0 Comment(2)
I've put together a post that describes an approach for using PATCH in Spring. And a working example is available on GitHub.Bithia
Also have a look at other possible solutions: Spring MVC PATCH method: partial updatesPhotomechanical
P
11

I do not think you can use the spring-data-rest functionality here.

spring-data-rest is using json-patch library internally. Basically I think the workflow would be as follows:

  • read your entity
  • convert it to json using the objectMapper
  • apply the patch (here you need json-patch) (I think your controller should take a list of JsonPatchOperation as input)
  • merge the patched json into your entity

I think the hard part is the fourth point. But if you do not have to have a generic solution it could be easier.

If you want to get an impression of what spring-data-rest does - look at org.springframework.data.rest.webmvc.config.JsonPatchHandler

EDIT

The patch mechanism in spring-data-rest changed significantly in the latest realeases. Most importantly it is no longer using the json-patch library and is now implementing json patch support from scratch.

I could manage to reuse the main patch functionality in a custom controller method.

The following snippet illustrates the approach based on spring-data-rest 2.6

        import org.springframework.data.rest.webmvc.IncomingRequest;
        import org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter;
        import org.springframework.data.rest.webmvc.json.patch.Patch;

        //...
        private final ObjectMapper objectMapper;
        //...

        @PatchMapping(consumes = "application/json-patch+json")
        public ResponseEntity<Void> patch(ServletServerHttpRequest request) {
          MyEntity entityToPatch = someRepository.findOne(id)//retrieve current state of your entity/object to patch

          Patch patch = convertRequestToPatch(request);
          patch.apply(entityToPatch, MyEntity.class);

          someRepository.save(entityToPatch);
          //...
        }      

        private Patch convertRequestToPatch(ServletServerHttpRequest request) {  
          try {
            InputStream inputStream =  new IncomingRequest(request).getBody();
            return new JsonPatchPatchConverter(objectMapper).convert(objectMapper.readTree(inputStream));
          } catch (IOException e) {
            throw new UncheckedIOException(e);
          }
        }
Procedure answered 23/10, 2015 at 8:7 Comment(2)
Thanks! Yes, I've dug through the SDR sources. It does seem like much of this functionality could be untangled from SDR itself for a more generic solution, though. It'd certainly make manually crafting HATEOAS compliant custom controllers easier.Wellfound
When I try this, I get java.io.IOException: Stream closed at objectMapper.readTree(inputStream) . Any idea why?Ultra

© 2022 - 2024 — McMap. All rights reserved.