title: How to convert ConstraintViolationException 500 error to 400 bad request?
body: <p>If I use a constraint like this @NotNull and then in the controller </p>
  
  <pre><code>public User createUser(
              @Validated
              @RequestBody User user) {}
  </code></pre>
  
  <p>It gives a really nice 400 exception with details. </p>
  
  <p>But if I use my own custom validator like this:</p>
  
  <pre><code>public User createUser(
              @UserConstraint
              @RequestBody User user) {}
  </code></pre>
  
  <p>It throws a 500 server error like this:</p>
  
  <pre><code>javax.validation.ConstraintViolationException: createUser.user: Error with field: 'test35'
      at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:117) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.10.RELEASE.jar:5.1.10.RELEASE]
      at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:69) ~[spring-security-core-5.1.6.RELEASE.jar:5.1.6.RELEASE]
  </code></pre>
  
  <p>Is there a way to get nice 400 message to the response?</p>
  
  <p>Ideally the 400 message should be the same as Spring's validation JSON</p>
  
  <pre><code>{
      "timestamp": "2019-10-30T02:33:15.489+0000",
      "status": 400,
      "error": "Bad Request",
      "errors": [
          {
              "codes": [
                  "Size.user.lastName",
                  "Size.lastName",
                  "Size.java.lang.String",
                  "Size"
              ],
              "arguments": [
                  {
                      "codes": [
                          "user.lastName",
                          "lastName"
                      ],
                      "arguments": null,
                      "defaultMessage": "lastName",
                      "code": "lastName"
                  },
                  25,
                  1
              ],
              "defaultMessage": "size must be between 1 and 25",
              "objectName": "user",
              "field": "lastName",
              "rejectedValue": "",
              "bindingFailure": false,
              "code": "Size"
          }
      ],
      "message": "Validation failed for object='user'. Error count: 1",
      "path": "/api/v1/users"
  }
  </code></pre>