All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.buabook.http.common.jersey.ConstraintViolationExceptionConverter Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.buabook.http.common.jersey;

import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.glassfish.jersey.server.validation.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;

/**
 * 

{@link ConstraintViolationException} to {@link ValidationError} Converter

* (c) 2016 Sport Trades Ltd * * @author Jas Rajasansir * @version 1.0.0 * @since 22 Dec 2016 */ public class ConstraintViolationExceptionConverter { private static final Logger log = LoggerFactory.getLogger(ConstraintViolationExceptionConverter.class); public static List asValidationErrors(ConstraintViolationException e) { log.warn("Jersey API input argument validation failed"); List validationErrors = Lists.newArrayList(); if(e == null || e.getConstraintViolations().isEmpty()) return validationErrors; Set> violations = e.getConstraintViolations(); log.warn("Constraint violation exception detected [ Violations: " + violations.size() + " ]"); for(ConstraintViolation violation : violations) { String propertyPath = (violation.getPropertyPath() == null) ? "" : violation.getPropertyPath().toString(); String invalidValue = (violation.getInvalidValue() == null) ? "" : violation.getInvalidValue().toString(); log.debug(" > Validation failure: " + propertyPath + " | " + violation.getMessage()); validationErrors.add( new ValidationError(violation.getMessage(), violation.getMessageTemplate(), propertyPath, invalidValue) ); } return validationErrors; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy