title: How do you use JSON Patch with Spring Data REST?
body: <p>According to the <a href="http://docs.spring.io/spring-data/rest/docs/current/reference/html/" rel="nofollow noreferrer">Sprind Data Rest Docs</a> it supports media type <code>application/patch+json</code>. Perhaps this assumption is wrong, but I take this to mean Spring Data Rest supports <a href="https://www.rfc-editor.org/rfc/rfc6902" rel="nofollow noreferrer">JSON Patch (RFC 6902)</a>. How do you get it to work?</p>
  <p>Here's what I've tried:</p>
  <p>Added these dependencies to my <code>build.gradle</code>:</p>
  <pre><code>compile('org.springframework.boot:spring-boot-starter-data-rest')
  compile('org.springframework.boot:spring-boot-starter-web')
  compile('org.springframework.data:spring-data-keyvalue')
  </code></pre>
  <p>Using the following code:</p>
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.data.annotation.Id;
  import org.springframework.data.map.repository.config.EnableMapRepositories;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.data.repository.query.Param;
  import org.springframework.data.rest.core.annotation.RepositoryRestResource;
  import org.springframework.data.rest.core.annotation.RestResource;
  import org.springframework.stereotype.Component;
  
  import java.util.List;
  import java.util.UUID;
  
  @SpringBootApplication
  @EnableMapRepositories
  public class SpringDataRest1Application {
    public static void main(String[] args) {
      SpringApplication.run(SpringDataRest1Application.class, args);
    }
  }
  
  @RepositoryRestResource
  interface UserRepository extends CrudRepository&lt;User, UUID&gt; {
  
    @RestResource(path = &quot;by-name&quot;)
    List&lt;User&gt; findByName(@Param(&quot;name&quot;) String name);
  
  }
  
  
  class User {
  
    @Id
    private UUID id = UUID.randomUUID();
    private String name;
    private int age;
  
  
    public User() {
    }
  
    public User(String name, int age) {
      this.name = name;
      this.age = age;
    }
  
    public String getName() {
      return name;
    }
  
    public void setName(String name) {
      this.name = name;
    }
  
    public User(UUID id) {
      this.id = id;
    }
  
    public int getAge() {
      return age;
    }
  
    public void setAge(int age) {
      this.age = age;
    }
  
    public UUID getId() {
      return id;
    }
  
    @Override
    public String toString() {
      final StringBuilder sb = new StringBuilder(&quot;User{&quot;);
      sb.append(&quot;id=&quot;).append(id);
      sb.append(&quot;, name='&quot;).append(name).append('\'');
      sb.append(&quot;, age=&quot;).append(age);
      sb.append('}');
      return sb.toString();
    }
  
  }
  
  @Component
  class SampleDataCLR implements CommandLineRunner {
  
    private UserRepository userRepository;
  
    @Autowired
    public SampleDataCLR(UserRepository userRepository) {
      this.userRepository = userRepository;
    }
  
    @Override
    public void run(String... args) throws Exception {
      userRepository.save(new User(&quot;Ethan&quot;, 10));
      userRepository.findAll().forEach(System.out::println);
    }
  }
  </code></pre>
  <p>To show it's working after running the above:</p>
  <pre><code>$ curl http://localhost:8080/users/55967eb0-d76e-4e62-9313-fe35d7cfa193
  {
    &quot;name&quot; : &quot;Ethan&quot;,
    &quot;age&quot; : 10,
    &quot;_links&quot; : {
      &quot;self&quot; : {
        &quot;href&quot; : &quot;http://localhost:8080/users/55967eb0-d76e-4e62-9313-fe35d7cfa193&quot;
      },
      &quot;user&quot; : {
        &quot;href&quot; : &quot;http://localhost:8080/users/55967eb0-d76e-4e62-9313-fe35d7cfa193&quot;
      }
    }
  }
  </code></pre>
  <p>When I attempt this call, which I believe conforms to <a href="http://jsonpatch.com/" rel="nofollow noreferrer">JSON Patch</a>:</p>
  <pre><code>curl --request PATCH localhost:8080/users/55967eb0-d76e-4e62-9313-fe35d7cfa193 --data '[{ &quot;op&quot;: &quot;replace&quot;, &quot;path&quot;: &quot;/age&quot;, &quot;value&quot;: &quot;15&quot; }]' -H &quot;Content-Type: application/patch+json&quot;
  </code></pre>
  <p>I get this error:</p>
  <pre><code>{&quot;cause&quot;:{&quot;cause&quot;:null,&quot;message&quot;:&quot;com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode&quot;},&quot;message&quot;:&quot;Could not read payload!; nested exception is java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode&quot;}
  </code></pre>
  <p>The logs show:</p>
  <pre><code>2017-03-08 13:30:59.869 ERROR 6852 --- [nio-8080-exec-2] o.s.d.r.w.RepositoryRestExceptionHandler : Could not read payload!; nested exception is java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
  
  org.springframework.http.converter.HttpMessageNotReadableException: Could not read payload!; nested exception is java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
      at org.springframework.data.rest.webmvc.json.DomainObjectReader.read(DomainObjectReader.java:89) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      at org.springframework.data.rest.webmvc.config.JsonPatchHandler.applyMergePatch(JsonPatchHandler.java:95) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      at org.springframework.data.rest.webmvc.config.JsonPatchHandler.apply(JsonPatchHandler.java:85) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.readPatch(PersistentEntityResourceHandlerMethodArgumentResolver.java:198) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.read(PersistentEntityResourceHandlerMethodArgumentResolver.java:176) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      at org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.resolveArgument(PersistentEntityResourceHandlerMethodArgumentResolver.java:138) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11]
      at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
  Caused by: java.lang.ClassCastException: com.fasterxml.jackson.databind.node.ArrayNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
      at org.springframework.data.rest.webmvc.json.DomainObjectReader.read(DomainObjectReader.java:87) ~[spring-data-rest-webmvc-2.6.1.RELEASE.jar:na]
      ... 54 common frames omitted
  </code></pre>