@PatchMapping

From NovaOrdis Knowledge Base
Revision as of 03:06, 13 March 2019 by Ovidiu (talk | contribs) (→‎REST)
Jump to navigation Jump to search

Internal

Overview

The annotation may also contain a sub-path relative to the class-level base path, usually configured with @RequestMapping.

REST

@RestController
@RequestMapping(path = "/a", produces = "application/json")
public class AController {

  @PatchMapping(path = "/{id}", consumes = "application/json")
  public ResponseEntity<A> patch(@PathVariable("id") Integer id, @RequestBody A a) {

    A current = content.get(id);

    if (current == null) {

       return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
   }

    if (a.getName() != null) {

        current.setName(a.getName());
    }

    ...
    return new ResponseEntity<>(current, HttpStatus.OK);
  }
}

Patching Semantics

The above implementation has limitations: if null values are meant to specify change, how the client should indicate that a filed should be set to null. Also, collections can only be replaced in bulk, there is no way of adding or removing a subsets of items from a collection.

HTTP PATCH Semantics for REST Applications

HTTP PATCH Semantics for REST Applications