title: Spring REST validation response details
body: <p>Trying to write simple test application using Spring REST and AngularJS using server-side Spring validation.</p>
  
  <p>In REST controller wrote such code:</p>
  
  <pre><code>    @RestController
      @RequestMapping("/people")
      public class PeopleResource {
  
      //...
  
      @InitBinder
      protected void initBinder(WebDataBinder binder) {
          binder.setValidator(new PeopleValidator());
      }
  
      @RequestMapping(method = RequestMethod.POST)
      @Transactional
      @ResponseBody
      public ResultDTO add(@Valid @RequestBody PeopleDTO peopleDTO) {
      // ...
      // some code to save to database
      // ...
          return new ResultDTO(id.toString());
     }
  }
  </code></pre>
  
  <p>And the validator:</p>
  
  <pre><code>public class PeopleValidator implements Validator {
  
      @Override
      public boolean supports(Class&lt;?&gt; clazz) {
          return PeopleDTO.class.equals(clazz);
      }
  
      @Override
      public void validate(Object target, Errors e) {
  
          PeopleDTO p = (PeopleDTO) target;
  
          if (p.getName().toLowerCase().equals("disallowedname")) {
              e.rejectValue("name", "Disallowed name");
          }
      }
  }
  </code></pre>
  
  <p>And code in AngularJS service and controller:</p>
  
  <pre><code>        //...
          function add(data) {
              return $resource('rs/people', {}, {'add': {method: 'POST'}}).add({}, data);
          }
          //...
  
          //...
          people.add(newPeople).$promise.then(
              function(){
                  $uibModalInstance.close();
              },
              function(res){
                  // do something to say there are problems
              }
          );
          //...
  </code></pre>
  
  <p>Ok, validator works fine. I get into PeopleValitator, get an error with message "Disallowed name".</p>
  
  <p>In the client-side code, response(res value in js code) looks like HTML containing message "HTTP ERROR 400" and nothing about specific error.</p>
  
  <p>Experienced people, for example <a href="http://www.leveluplunch.com/java/tutorials/017-validate-spring-rest-webservice-request/" rel="nofollow">here</a>, write it would be response like JSON containing specific error information, but I can't get such behavior, maybe doing something wrong.</p>
  
  <p>Also here could used BindingResult as a paramether in REST method, it works, but I think it's not best idea.</p>
  
  <p>Please, help, what should I do in this sample to get validation error information on the client side.</p>