Skip to content


Introducing MethodArgumentNotValidException used when a bean validati…
Browse files Browse the repository at this point in the history
…on fails. This exception wraps bean validation violations, and is backward compatible with previous behaviour (extending IllegalArgumentException)
  • Loading branch information
Thomas Zayouna committed Oct 7, 2015
1 parent be01892 commit bb91412
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package restx.validation;


import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.metadata.ConstraintDescriptor;
import java.lang.annotation.ElementType;
import java.util.Set;

import static;

* Created by Thomas Zayouna on 07/10/15.
* Exception wrapping bean validation violations
public class MethodArgumentNotValidException extends IllegalArgumentException {

public static class ViolationContent {
private final String message;
private final Path propertyPath;
private final Class rootBeanClass;

public ViolationContent(String message, Path propertyPath, Class rootBeanClass) {
this.message = message;
this.propertyPath = propertyPath;
this.rootBeanClass = rootBeanClass;

public String getMessage() {
return message;

public Path getPropertyPath() {
return propertyPath;

public Class getRootBeanClass() {
return rootBeanClass;

public static Function<ConstraintViolation, ViolationContent> VIOLATION_CONTENT_EXTRACTOR = new Function<ConstraintViolation, ViolationContent>() {
public ViolationContent apply(ConstraintViolation input) {
return new ViolationContent(input.getMessage(), input.getPropertyPath(), input.getRootBeanClass());

private final Set<ViolationContent> violations;

public <T> MethodArgumentNotValidException(Set<ConstraintViolation<T>> violations) {
// Kept for backward compat for restx <= 0.34
this.violations = newHashSet(Collections2.transform(violations, VIOLATION_CONTENT_EXTRACTOR));

public Set<ViolationContent> getViolations() {
return violations;
2 changes: 1 addition & 1 deletion restx-core/src/main/java/restx/validation/
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static <T> T checkValid(Optional<Validator> validator, T o, Class... grou

Set<ConstraintViolation<T>> violations = validator.get().validate(o, groups);
if (!violations.isEmpty()) {
throw new IllegalArgumentException(Joiner.on(", ").join(violations));
throw new MethodArgumentNotValidException(violations);

Expand Down

0 comments on commit bb91412

Please sign in to comment.