Skip to content

Commit 9f211de

Browse files
committed
Turn MethodArgumentNotValidException into subclass of BindException
Closes gh-23107
1 parent 8a394c2 commit 9f211de

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

spring-context/src/main/java/org/springframework/validation/BindException.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,8 +70,6 @@ public BindException(Object target, String objectName) {
7070

7171
/**
7272
* Return the BindingResult that this BindException wraps.
73-
* Will typically be a BeanPropertyBindingResult.
74-
* @see BeanPropertyBindingResult
7573
*/
7674
public final BindingResult getBindingResult() {
7775
return this.bindingResult;

spring-web/src/main/java/org/springframework/web/bind/MethodArgumentNotValidException.java

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,58 +17,53 @@
1717
package org.springframework.web.bind;
1818

1919
import org.springframework.core.MethodParameter;
20+
import org.springframework.validation.BindException;
2021
import org.springframework.validation.BindingResult;
2122
import org.springframework.validation.ObjectError;
2223

2324
/**
2425
* Exception to be thrown when validation on an argument annotated with {@code @Valid} fails.
26+
* Extends {@link BindException} as of 5.3.
2527
*
2628
* @author Rossen Stoyanchev
29+
* @author Juergen Hoeller
2730
* @since 3.1
2831
*/
2932
@SuppressWarnings("serial")
30-
public class MethodArgumentNotValidException extends Exception {
33+
public class MethodArgumentNotValidException extends BindException {
3134

3235
private final MethodParameter parameter;
3336

34-
private final BindingResult bindingResult;
35-
3637

3738
/**
3839
* Constructor for {@link MethodArgumentNotValidException}.
3940
* @param parameter the parameter that failed validation
4041
* @param bindingResult the results of the validation
4142
*/
4243
public MethodArgumentNotValidException(MethodParameter parameter, BindingResult bindingResult) {
44+
super(bindingResult);
4345
this.parameter = parameter;
44-
this.bindingResult = bindingResult;
4546
}
4647

48+
4749
/**
4850
* Return the method parameter that failed validation.
4951
*/
50-
public MethodParameter getParameter() {
52+
public final MethodParameter getParameter() {
5153
return this.parameter;
5254
}
5355

54-
/**
55-
* Return the results of the failed validation.
56-
*/
57-
public BindingResult getBindingResult() {
58-
return this.bindingResult;
59-
}
60-
61-
6256
@Override
6357
public String getMessage() {
6458
StringBuilder sb = new StringBuilder("Validation failed for argument [")
6559
.append(this.parameter.getParameterIndex()).append("] in ")
6660
.append(this.parameter.getExecutable().toGenericString());
67-
if (this.bindingResult.getErrorCount() > 1) {
68-
sb.append(" with ").append(this.bindingResult.getErrorCount()).append(" errors");
61+
BindingResult bindingResult = getBindingResult();
62+
if (bindingResult.getErrorCount() > 1) {
63+
sb.append(" with ").append(bindingResult.getErrorCount()).append(" errors");
6964
}
7065
sb.append(": ");
71-
for (ObjectError error : this.bindingResult.getAllErrors()) {
66+
for (ObjectError error : bindingResult.getAllErrors()) {
7267
sb.append("[").append(error).append("] ");
7368
}
7469
return sb.toString();

0 commit comments

Comments
 (0)