@SuppressWarnings({ "unchecked", "rawtypes" }) @Test public void constraintViolationExceptionCausesBadRequest() { Path violationPath = mock(Path.class); when(violationPath.toString()).thenReturn("violationPath"); ConstraintViolation violation = mock(ConstraintViolation.class); when(violation.getRootBeanClass()).thenReturn(CustomValidationExceptionMapperTest.class); when(violation.getPropertyPath()).thenReturn(violationPath); when(violation.getMessage()).thenReturn("violationMessage"); ConstraintViolationException exception = mock(ConstraintViolationException.class); when(exception.getConstraintViolations()).thenReturn(new LinkedHashSet(asList(violation))); try (Response response = exceptionMapper.toResponse(exception)) { assertThat(response.getStatus(), is(BAD_REQUEST.getStatusCode())); ErrorResponse error = (ErrorResponse) response.getEntity(); assertThat(error.error, is("violationPath: violationMessage")); } }
@Override public Response toResponse(ValidationException exception) { if (exception instanceof ConstraintViolationException && !(exception instanceof ResponseConstraintViolationException)) { ConstraintViolationException constraint = (ConstraintViolationException) exception; String error = constraint.getConstraintViolations().stream().map(violation -> { logger.warn("{}.{}: {}", violation.getRootBeanClass().getSimpleName(), violation.getPropertyPath(), violation.getMessage()); return violation.getPropertyPath() + ": " + violation.getMessage(); }).collect(joining(", ")); return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse(error)).build(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse(exception.getMessage())).build(); }
CustomValidationExceptionMapper implements ExceptionMapper<ValidationException> { @Override public Response toResponse(ValidationException exception) { if (exception instanceof ConstraintViolationException && !(exception instanceof ResponseConstraintViolationException)) { ConstraintViolationException constraint = (ConstraintViolationException) exception; String error = constraint.getConstraintViolations().stream().map(violation -> { logger.warn("{}.{}: {}", violation.getRootBeanClass().getSimpleName(), violation.getPropertyPath(), violation.getMessage()); return violation.getPropertyPath() + ": " + violation.getMessage(); }).collect(joining(", ")); return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse(error)).build(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse(exception.getMessage())).build(); } }
CustomValidationExceptionMapper implements ExceptionMapper<ValidationException> { @Override public Response toResponse(ValidationException exception) { if (exception instanceof ConstraintViolationException && !(exception instanceof ResponseConstraintViolationException)) { ConstraintViolationException constraint = (ConstraintViolationException) exception; String error = constraint.getConstraintViolations().stream().map(violation -> { logger.warn("{}.{}: {}", violation.getRootBeanClass().getSimpleName(), violation.getPropertyPath(), violation.getMessage()); return violation.getPropertyPath() + ": " + violation.getMessage(); }).collect(joining(", ")); return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse(error)).build(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse(exception.getMessage())).build(); }  }
CustomValidationExceptionMapper implements ExceptionMapper<ValidationException> { @Override public Response toResponse(ValidationException exception) { if (exception instanceof ConstraintViolationException && !(exception instanceof ResponseConstraintViolationException)) { ConstraintViolationException constraint = (ConstraintViolationException) exception; String error = constraint.getConstraintViolations().stream().map(violation -> { logger.warn("{}.{}: {}", violation.getRootBeanClass().getSimpleName(), violation.getPropertyPath(), violation.getMessage()); return violation.getPropertyPath() + ": " + violation.getMessage(); }).collect(joining(", ")); return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse(error)).build(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse(exception.getMessage())).build(); }  @Override Response toResponse(ValidationException exception); }
CustomValidationExceptionMapper implements ExceptionMapper<ValidationException> { @Override public Response toResponse(ValidationException exception) { if (exception instanceof ConstraintViolationException && !(exception instanceof ResponseConstraintViolationException)) { ConstraintViolationException constraint = (ConstraintViolationException) exception; String error = constraint.getConstraintViolations().stream().map(violation -> { logger.warn("{}.{}: {}", violation.getRootBeanClass().getSimpleName(), violation.getPropertyPath(), violation.getMessage()); return violation.getPropertyPath() + ": " + violation.getMessage(); }).collect(joining(", ")); return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse(error)).build(); } return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(new ErrorResponse(exception.getMessage())).build(); }  @Override Response toResponse(ValidationException exception);  }