DataValidationExceptionMapper implements ExceptionMapper<DataValidationException> { @Override public Response toResponse(DataValidationException e) { return Response.status(400) .type(MediaType.APPLICATION_JSON_TYPE) .entity(ImmutableMap.of("code", 400, "message", e.getMessage(), "errors", e.getErrors())) .build(); }  @Override Response toResponse(DataValidationException e);  }
@Test @SuppressWarnings("unchecked") public void toResponse() throws Exception { DataValidationExceptionMapper mapper = new DataValidationExceptionMapper(); DataValidationException exception = new DataValidationException(Arrays.asList(ValidationError.of("field", "is invalid"))); Response response = mapper.toResponse(exception); assertEquals(400, response.getStatus()); assertEquals("application/json", response.getMediaType().toString()); Map<String,Object> map = (Map<String,Object>)response.getEntity(); assertEquals(400, map.get("code")); assertEquals("field: is invalid", map.get("message")); assertEquals("field", ((List<ValidationError>)map.get("errors")).get(0).getField()); }