title: How to return an error message in JSON text when my simple REST get fails?
body: <p>I'm building a web app with Bootstrap, jQuery, and Spring MVC.</p>
  
  <p>I have a web service defined that looks like this and it works fine ... the User object is returned in JSON format.</p>
  
  <p>However, when there is an error, I'd like to return something that looks like this ...</p>
  
  <pre><code>{
    "success": false,
    "message": "Unable to retrieve user for id '55'; Could not connect to database."
  }
  </code></pre>
  
  <p>... so I can parse out the error message and display it in a nice alert.</p>
  
  <p>I tried throwing an Exception, but that didn't work.  What else should I try?  Here is the code ...</p>
  
  <pre><code>@RestController
  public class UserController {
  
  /* ... */
  
      @RequestMapping(value = "/json/user/{userId}", method = RequestMethod.GET)
      @ResponseBody
      public User getUser(@PathVariable("userId") int userId) throws Exception {
          boolean bSuccess = false;
          String errorMsg = "";
  
          User user = null;
          try {
              user = userService.getUser(userId);
              bSuccess = true
          } catch (Exception x) {
              bSuccess = false;
              errorMsg = "Unable to retrieve user for id '"+userId+"'; "+x.getMessage();
          }
  
          // Need something here; 
          // if (!bSuccess) {
          //    return json error message ??
          // }
  
          return user;
      }
  
  /* ... */
  
  }
  </code></pre>