Search in sources :

Example 1 with ValidationProvider

use of javax.validation.spi.ValidationProvider in project jo-client-platform by jo-source.

the class HibernateBeanValidatorFactory method create.

@Override
public Validator create() {
    final ValidationProviderResolver validationProviderResolver = new ValidationProviderResolver() {

        @Override
        public List<ValidationProvider<?>> getValidationProviders() {
            final List<ValidationProvider<?>> result = new LinkedList<ValidationProvider<?>>();
            result.add(new HibernateValidator());
            return result;
        }
    };
    final ProviderSpecificBootstrap<HibernateValidatorConfiguration> bootstrap = Validation.byProvider(HibernateValidator.class);
    bootstrap.providerResolver(validationProviderResolver);
    final HibernateValidatorConfiguration validatorConfiguration = bootstrap.configure();
    return validatorConfiguration.buildValidatorFactory().getValidator();
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidationProvider(javax.validation.spi.ValidationProvider) ValidationProviderResolver(javax.validation.ValidationProviderResolver) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) LinkedList(java.util.LinkedList)

Example 2 with ValidationProvider

use of javax.validation.spi.ValidationProvider in project cxf by apache.

the class BookStore method createBookValidate.

@POST
@Path("/books-validate")
public Response createBookValidate(Book book) {
    assertInjections();
    BeanValidationProvider prov = new BeanValidationProvider(new ValidationProviderResolver() {

        @Override
        public List<ValidationProvider<?>> getValidationProviders() {
            ValidationProvider<HibernateValidatorConfiguration> prov = new HibernateValidator();
            List<ValidationProvider<?>> provs = new ArrayList<>();
            provs.add(prov);
            return provs;
        }
    }, HibernateValidator.class);
    try {
        prov.validateBean(book);
    } catch (ConstraintViolationException cve) {
        StringBuilder violationMessages = new StringBuilder();
        for (ConstraintViolation<?> constraintViolation : cve.getConstraintViolations()) {
            violationMessages.append(constraintViolation.getPropertyPath()).append(": ").append(constraintViolation.getMessage()).append("\n");
        }
        return Response.status(Response.Status.BAD_REQUEST).type("text/plain").entity(violationMessages.toString()).build();
    }
    return createBook(book);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) BeanValidationProvider(org.apache.cxf.validation.BeanValidationProvider) ValidationProvider(javax.validation.spi.ValidationProvider) ConstraintViolation(javax.validation.ConstraintViolation) ValidationProviderResolver(javax.validation.ValidationProviderResolver) ConstraintViolationException(javax.validation.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) BeanValidationProvider(org.apache.cxf.validation.BeanValidationProvider) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

ValidationProviderResolver (javax.validation.ValidationProviderResolver)2 ValidationProvider (javax.validation.spi.ValidationProvider)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 BeanValidationProvider (org.apache.cxf.validation.BeanValidationProvider)1 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)1