|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.web.bind;
|
18 | 18 |
|
19 | 19 | import org.springframework.core.MethodParameter;
|
| 20 | +import org.springframework.validation.BindException; |
20 | 21 | import org.springframework.validation.BindingResult;
|
21 | 22 | import org.springframework.validation.ObjectError;
|
22 | 23 |
|
23 | 24 | /**
|
24 | 25 | * Exception to be thrown when validation on an argument annotated with {@code @Valid} fails.
|
| 26 | + * Extends {@link BindException} as of 5.3. |
25 | 27 | *
|
26 | 28 | * @author Rossen Stoyanchev
|
| 29 | + * @author Juergen Hoeller |
27 | 30 | * @since 3.1
|
28 | 31 | */
|
29 | 32 | @SuppressWarnings("serial")
|
30 |
| -public class MethodArgumentNotValidException extends Exception { |
| 33 | +public class MethodArgumentNotValidException extends BindException { |
31 | 34 |
|
32 | 35 | private final MethodParameter parameter;
|
33 | 36 |
|
34 |
| - private final BindingResult bindingResult; |
35 |
| - |
36 | 37 |
|
37 | 38 | /**
|
38 | 39 | * Constructor for {@link MethodArgumentNotValidException}.
|
39 | 40 | * @param parameter the parameter that failed validation
|
40 | 41 | * @param bindingResult the results of the validation
|
41 | 42 | */
|
42 | 43 | public MethodArgumentNotValidException(MethodParameter parameter, BindingResult bindingResult) {
|
| 44 | + super(bindingResult); |
43 | 45 | this.parameter = parameter;
|
44 |
| - this.bindingResult = bindingResult; |
45 | 46 | }
|
46 | 47 |
|
| 48 | + |
47 | 49 | /**
|
48 | 50 | * Return the method parameter that failed validation.
|
49 | 51 | */
|
50 |
| - public MethodParameter getParameter() { |
| 52 | + public final MethodParameter getParameter() { |
51 | 53 | return this.parameter;
|
52 | 54 | }
|
53 | 55 |
|
54 |
| - /** |
55 |
| - * Return the results of the failed validation. |
56 |
| - */ |
57 |
| - public BindingResult getBindingResult() { |
58 |
| - return this.bindingResult; |
59 |
| - } |
60 |
| - |
61 |
| - |
62 | 56 | @Override
|
63 | 57 | public String getMessage() {
|
64 | 58 | StringBuilder sb = new StringBuilder("Validation failed for argument [")
|
65 | 59 | .append(this.parameter.getParameterIndex()).append("] in ")
|
66 | 60 | .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"); |
69 | 64 | }
|
70 | 65 | sb.append(": ");
|
71 |
| - for (ObjectError error : this.bindingResult.getAllErrors()) { |
| 66 | + for (ObjectError error : bindingResult.getAllErrors()) { |
72 | 67 | sb.append("[").append(error).append("] ");
|
73 | 68 | }
|
74 | 69 | return sb.toString();
|
|
0 commit comments