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?
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.
@Path("/admin") public class AdminService { @Context HttpServletRequest request; @POST @Produces(MediaType.APPLICATION_JSON) public Map<Integer, String> createCompany(Company company){ Map<Integer, String> map = new HashMap<>(); 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; } } }