title: REST service: The proper way of returning exception
body: <p>I use Jersey API for my REST service. My question is: Is there a more elegant way of returning exceptions in a JSON form? Is it better to concern myself with creating a json object myself and attaching it to the response directly?</p>
  
  <p>This is a simplified example of one of the methods in the service. As you see, I use HashMap only because the method may throw an exception, in which case I need to return information about It.</p>
  
  <pre><code>@Path("/admin")
  public class AdminService {
  
      @Context
      HttpServletRequest request;
  
      @POST
      @Produces(MediaType.APPLICATION_JSON)
      public Map&lt;Integer, String&gt; createCompany(Company company){
  
          Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;();
          try{
              AdminFacade adminFacade = (AdminFacade)Utility.getFacade(request);
              adminFacade.createCompany(company);
              map.put(1,"success");
          } catch (ExceptionREST e) {
              map.put(e.getErrorNumber(), e.getMessage());
          } finally {
              return map;
          }
      }
  }
  </code></pre>