title: How Spring Data REST return hibernate-JPA validation & server internal errors in json not stack trace
body: <p>I want to get JPA validation and server internal error on <code>JSON</code> format and not stack trace,</p>
  
  <p>Using <code>Spring JPA data rest</code></p>
  
  <ul>
  <li><p><strong>MyEntity Repository</strong></p>
  
  <pre><code>@RepositoryRestResource(path = "entity")
  public interface MyEntityRepo extends CrudRepository&lt;MyEntity, Long&gt; {}
  </code></pre></li>
  <li><p><strong>When post without <code>name, address</code> property i expect NOT NULL validation error.</strong></p>
  
  <pre><code>curl -i -X POST -H "Content-Type: application/json" -d '{"name":"not passed and @NotNull","address":"not passed and @NotNull","city":"city","area":"area"}' http://localhost:8080/clinicfinder/api/entity
  </code></pre></li>
  <li><p><strong>Current return :</strong></p></li>
  </ul>
  
  <blockquote>
    <p>HTTP Status 500 - Request processing failed; nested exception is org.springframework.data.rest.core.RepositoryConstraintViolationException:Validation failed
        org.springframework.data.rest.core.event.ValidatingRepositoryEventListener.validate(ValidatingRepositoryEventListener.java:179)
        org.springframework.data.rest.core.event.ValidatingRepositoryEventListener.onBeforeCreate(ValidatingRepositoryEventListener.java:96)
        org.springframework.data.rest.core.event.AbstractRepositoryEventListener.onApplicationEvent(AbstractRepositoryEventListener.java:50)
        org.springframework.data.rest.core.event.AbstractRepositoryEventListener.onApplicationEvent(AbstractRepositoryEventListener.java:29)
        org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
        org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
        org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
        org.springframework.data.rest.webmvc.RepositoryEntityController.createAndReturn(RepositoryEntityController.java:484)
        org.springframework.data.rest.webmvc.RepositoryEntityController.postCollectionResource(RepositoryEntityController.java:272)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:483)
        org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
        org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
        org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
        org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
        org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
        org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)</p>
  </blockquote>
  
  <ul>
  <li><p><strong>Needed Return:</strong></p>
  
  <pre><code>response 404, {"errors":[{"entity":"MyEntity","message":"may not be null","invalidValue":"null","property":"name"},{"entity":"MyEntity","message":"may not be null","invalidValue":"null","property":"address"}]}
  </code></pre></li>
  <li><p><strong>Pom</strong></p>
  
  <pre><code>   &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;maven.compiler.source&gt;1.6&lt;/maven.compiler.source&gt;
      &lt;maven.compiler.target&gt;1.6&lt;/maven.compiler.target&gt;
      &lt;spring.data.jpa.version&gt;1.11.1.RELEASE&lt;/spring.data.jpa.version&gt;
  &lt;spring.data.rest.webmvc.version&gt;2.6.1.RELEASE&lt;/spring.data.rest.webmvc.version&gt;
     &lt;/properties&gt;
  &lt;dependencies&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;mysql&lt;/groupId&gt;
      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;version&gt;5.1.41&lt;/version&gt;
      &lt;scope&gt;runtime&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
      &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
      &lt;version&gt;3.0.1&lt;/version&gt;
      &lt;scope&gt;provided&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;!-- Spring Rest Repository --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
      &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
      &lt;version&gt;${spring.data.jpa.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
      &lt;artifactId&gt;spring-data-rest-webmvc&lt;/artifactId&gt;
      &lt;version&gt;${spring.data.rest.webmvc.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
      &lt;version&gt;5.2.10.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
      &lt;version&gt;5.4.1.Final&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p></p></li>
  <li><p><strong>web.xml</strong></p>
  
  <pre><code>&lt;listener&gt;
  &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring-config.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.data.rest.webmvc.RepositoryRestDispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;throwExceptionIfNoHandlerFound&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;rest&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre></li>
  <li><p><strong>According to this <a href="https://stackoverflow.com/a/35338751/1283715">Answer</a>:</strong></p></li>
  </ul>
  
  <p>Add this lines to <code>Spring-config.xml</code></p>
  
  <pre><code>&lt;context:annotation-config/&gt;
  &lt;bean class="path.to.config.RestValidationConfiguration" /&gt;
  
  @Configuration
  public class RestValidationConfiguration extends RepositoryRestConfigurerAdapter {
  
      @Bean
      @Primary
      /**
       * Create a validator to use in bean validation - primary to be able to
       * Autowire without qualifier
       */
      Validator validator() {
          return new LocalValidatorFactoryBean();
      }
  
      @Override
      public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
          Validator validator = validator();
          // bean validation always before save and create
          validatingListener.addValidator("beforeCreate", validator);
          validatingListener.addValidator("beforeSave", validator);
      }
  }
  </code></pre>
  
  <p><strong>But server keep returning <code>Stack trace</code> no <code>JSON</code> error format.</strong></p>
  
  <p>Any help will be appreciated, Thanks.</p>