title: How to return a error message in a method that return a ResponseEntity in Spring MVC using @ControllerAdvice
body: <p>I'm working on a web app using Spring MVC and AngularJS, I'm creating a Rest API that returns ResponseEntities that contains JSON strings. </p>
  
  <p>I want to be able when an Exception happens to return a string that contains the error cause to my view and then show this error with a modal in AngularJS, I created a Class with the <code>@ControllerAdvice</code> annotation and in this class I defined a method with my custom exception like this </p>
  
  <pre><code>@ControllerAdvice
  public class GlobalExceptionHandlerController {
  
  @ExceptionHandler(PersonalException.class)
  public String handleCustomExceptionRazon(PersonalException ex) {
  
      String errorMessage = "custom error";
  
  
      return errorMessage;
  }
  }
  </code></pre>
  
  <p>I have the following interface</p>
  
  <pre><code>public interface ClientDAO {
  
  public void insertCLiente(Client client) throws PersonalException
  }
  </code></pre>
  
  <p>And in my implementation </p>
  
  <pre><code>@Override
  public void insertCLiente(Client client) throws PersonalException{
  
  //method implementation
   if (searchCLiente(client.name())) {
          throw new PersonalException("client aleady exists");
      } else {
  //method implementation
  }
  }
  </code></pre>
  
  <p>My searchClient Method</p>
  
  <pre><code>public boolean searchClient(String name) {
  try {
      //method implementation
  
  } catch (DataAccessException dataAccessException) {
          System.out.println("");
          dataAccessException.printStackTrace();
      } catch (Exception e) {
          System.out.println("");
          e.printStackTrace();
      }
    //method implementation
  }
  </code></pre>
  
  <p>My Client Controller</p>
  
  <pre><code>@Autowired
      ClientDAO clientDAO;
  
  @RequestMapping(value = "/client/", method = RequestMethod.POST)
  public ResponseEntity&lt;Void&gt; createClient(@RequestBody final String DTOClientData, UriComponentsBuilder ucBuilder) {
  
  //here I parse the JSON data and create my Client object
  
     //here I dont know how can I return the error message
  
  clientDAO.insertClient(client);
  
      }
  </code></pre>
  
  <p>My custom Exception</p>
  
  <pre><code>public class PersonalException extends Exception {
  
  public PersonalException (String msg) {
      super(msg);
  }
  
  }
  </code></pre>
  
  <p>I don't know un my clientController method createClient how can I return an execption of the type PersonalException that I created </p>