org.glassfish.jersey.server.validation.ValidationError Java Examples
The following examples show how to use
org.glassfish.jersey.server.validation.ValidationError.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JerseyValidationTest.java From parsec-libraries with Apache License 2.0 | 6 votes |
/** * Test to see that the message "Got it!" is sent in the response. */ @Test public void testGetItValidationError() { Response resp = target().path("/myresource/id") .queryParam("key1", "2") .queryParam("key2", "") .request(MediaType.APPLICATION_JSON_TYPE).get(); int code = resp.getStatus(); ParsecErrorResponse err = resp.readEntity(new GenericType<ParsecErrorResponse<ValidationError>>() { }); assertEquals(code, BAD_REQUEST.getStatusCode()); assertValidationErrorEquals( err, 40001, "constraint violation validate error unittest", Arrays.asList("MyResource.getIt.namedValue1", "MyResource.getIt.namedValue2", "MyResource.getIt.value3") ); }
Example #2
Source File: JerseyValidationTest.java From parsec-libraries with Apache License 2.0 | 6 votes |
@Test public void testPostItValidationError() { MyDto dto = new MyDto(); MySubDto subDto = new MySubDto(); dto.setSubClass(subDto); Response resp = target().path("/myresource/id") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(dto)); int code = resp.getStatus(); ParsecErrorResponse err = resp.readEntity(new GenericType<ParsecErrorResponse<ValidationError>>(){}); assertEquals(code, BAD_REQUEST.getStatusCode()); assertValidationErrorEquals( err, 40001, "constraint violation validate error unittest", Arrays.asList("MyResource.postIt.dto.subClass.nickname", "MyResource.postIt.dto.firstName", "MyResource.postIt.value1", "MyResource.postIt.dto.lastName", "MyResource.postIt.dto.subClass.birthday") ); }
Example #3
Source File: JerseyValidationTest.java From parsec-libraries with Apache License 2.0 | 5 votes |
/** * assert parsec error response */ private void assertValidationErrorEquals( ParsecErrorResponse parsecError, int expectErrCode, String expectMessage, List<String> expectPaths) { assertEquals(parsecError.getError().getCode(), expectErrCode, "error code not equals"); assertEquals(parsecError.getError().getMessage(), expectMessage, "error message not equals"); assertEquals(parsecError.getError().getDetail().size(), expectPaths.size(), "error detail count not equals"); List<String> errPaths = new ArrayList<>(); for (Object obj : parsecError.getError().getDetail()){ errPaths.add(((ValidationError) obj).getPath()); } assertEqualsNoOrder(errPaths.toArray(), expectPaths.toArray(), "error paths not equals"); }
Example #4
Source File: ValidationExceptionMapper.java From jaxrs-beanvalidation-javaee7 with Apache License 2.0 | 5 votes |
@Override public Response toResponse(final ValidationException exception) { if (exception instanceof ConstraintViolationException) { LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), exception); final ConstraintViolationException cve = (ConstraintViolationException) exception; final Response.ResponseBuilder response = Response.status(getStatus(cve)); // Entity final List<Variant> variants = Variant.mediaTypes( MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build(); final Variant variant = request.get().selectVariant(variants); if (variant != null) { response.type(variant.getMediaType()); } else { /* * default media type which will be used only when none media type from {@value variants} is in * accept header of original request. */ response.type(MediaType.TEXT_PLAIN_TYPE); } response.entity( new GenericEntity<List<ValidationError>>( getEntity(cve.getConstraintViolations()), new GenericType<List<ValidationError>>() {}.getType() ) ); return response.build(); } else { LOGGER.log(Level.WARNING, LocalizationMessages.VALIDATION_EXCEPTION_RAISED(), exception); return Response.serverError().entity(exception.getMessage()).build(); } }
Example #5
Source File: ValidationExceptionMapper.java From jaxrs-beanvalidation-javaee7 with Apache License 2.0 | 5 votes |
private List<ValidationError> getEntity(final Set<ConstraintViolation<?>> violations) { final List<ValidationError> errors = new ArrayList<ValidationError>(); for (final ConstraintViolation<?> violation : violations) { errors.add(new ValidationError(violation.getMessage(), violation.getMessageTemplate(), getPath(violation), getInvalidValue(violation.getInvalidValue()))); } return errors; }
Example #6
Source File: AlpineResource.java From Alpine with Apache License 2.0 | 4 votes |
/** * Accepts the result from one of the many validation methods available and * returns a List of ValidationErrors. If the size of the List is 0, no errors * were encounter during validation. * * Usage: * <pre> * Validator validator = getValidator(); * List<ValidationError> errors = contOnValidationError( * validator.validateProperty(myObject, "uuid"), * validator.validateProperty(myObject, "name") * ); * // If validation fails, this line will be reached. * </pre> * * @param violationsArray a Set of one or more ConstraintViolations * @return a List of zero or more ValidationErrors * @since 1.0.0 */ @SafeVarargs protected final List<ValidationError> contOnValidationError(final Set<ConstraintViolation<Object>>... violationsArray) { final List<ValidationError> errors = new ArrayList<>(); for (final Set<ConstraintViolation<Object>> violations : violationsArray) { for (final ConstraintViolation violation : violations) { if (violation.getPropertyPath().iterator().next().getName() != null) { final String path = violation.getPropertyPath() != null ? violation.getPropertyPath().toString() : null; final String message = violation.getMessage() != null ? StringUtils.removeStart(violation.getMessage(), path + ".") : null; final String messageTemplate = violation.getMessageTemplate(); final String invalidValue = violation.getInvalidValue() != null ? violation.getInvalidValue().toString() : null; final ValidationError error = new ValidationError(message, messageTemplate, path, invalidValue); errors.add(error); } } } return errors; }
Example #7
Source File: AlpineResource.java From Alpine with Apache License 2.0 | 3 votes |
/** * Wrapper around {@link #contOnValidationError(Set[])} but instead of returning * a list of errors, this method will halt processing of the request by throwing * a BadRequestException, setting the HTTP status to 400 (BAD REQUEST) and providing * a full list of validation errors in the body of the response. * * Usage: * <pre> * Validator validator = getValidator(); * failOnValidationError( * validator.validateProperty(myObject, "uuid"), * validator.validateProperty(myObject, "name") * ); * // If validation fails, this line will not be reached. * </pre> * * @param violationsArray a Set of one or more ConstraintViolations * @since 1.0.0 */ @SafeVarargs protected final void failOnValidationError(final Set<ConstraintViolation<Object>>... violationsArray) { final List<ValidationError> errors = contOnValidationError(violationsArray); if (! Collections.isEmpty(errors)) { throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build()); } }
Example #8
Source File: ParsecValidationExceptionMapper.java From parsec-libraries with Apache License 2.0 | 3 votes |
/** * implement toResponse with code, message. * * @param e the validation exception object * @param code the code * @param message the message * * @return the response */ private Response toResponse(ConstraintViolationException e, int code, String message) { LOGGER.log(Level.FINER, LocalizationMessages.CONSTRAINT_VIOLATIONS_ENCOUNTERED(), e); List<ValidationError> errors = ValidationHelper.constraintViolationToValidationErrors(e); ParsecErrorResponse<ValidationError> errorResponse = ValidateUtil.buildErrorResponse(errors, code, message); return Response.status(Response.Status.BAD_REQUEST) .entity(new GenericEntity<ParsecErrorResponse<ValidationError>>(errorResponse) { }).build(); }
Example #9
Source File: ParsecValidateError.java From parsec-libraries with Apache License 2.0 | 2 votes |
/** * Gets the detail. * * @return the detail */ public List<ValidationError> getDetail() { return detail; }
Example #10
Source File: ParsecValidateError.java From parsec-libraries with Apache License 2.0 | 2 votes |
/** * Sets the detail. * * @param detail the detail to set */ public void setDetail(List<ValidationError> detail) { this.detail = detail; }