1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2023 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.
16
16
17
17
package org .springframework .web .bind ;
18
18
19
+ import org .springframework .core .MethodParameter ;
20
+ import org .springframework .lang .Nullable ;
21
+
19
22
/**
20
23
* {@link ServletRequestBindingException} subclass that indicates a missing parameter.
21
24
*
@@ -29,14 +32,38 @@ public class MissingServletRequestParameterException extends MissingRequestValue
29
32
30
33
private final String parameterType ;
31
34
35
+ @ Nullable
36
+ private final MethodParameter parameter ;
37
+
32
38
33
39
/**
34
40
* Constructor for MissingServletRequestParameterException.
35
41
* @param parameterName the name of the missing parameter
36
42
* @param parameterType the expected type of the missing parameter
37
43
*/
38
44
public MissingServletRequestParameterException (String parameterName , String parameterType ) {
39
- this (parameterName , parameterType , false );
45
+ super ("" , false , null , new Object [] {parameterName });
46
+ this .parameterName = parameterName ;
47
+ this .parameterType = parameterType ;
48
+ this .parameter = null ;
49
+ getBody ().setDetail (initBodyDetail (this .parameterName ));
50
+ }
51
+
52
+ /**
53
+ * Constructor with a {@link MethodParameter} instead of a String parameterType.
54
+ * @param parameterName the name of the missing parameter
55
+ * @param parameter the target method parameter for the missing value
56
+ * @param missingAfterConversion whether the value became null after conversion
57
+ * @since 6.1
58
+ */
59
+ public MissingServletRequestParameterException (
60
+ String parameterName , MethodParameter parameter , boolean missingAfterConversion ) {
61
+
62
+ super ("" , missingAfterConversion , null , new Object [] {parameterName });
63
+ this .parameterName = parameterName ;
64
+ this .parameterType = parameter .getNestedParameterType ().getSimpleName ();
65
+ this .parameter = parameter ;
66
+ getBody ().setDetail (initBodyDetail (this .parameterName ));
40
67
}
41
68
42
69
/**
@@ -45,14 +72,21 @@ public MissingServletRequestParameterException(String parameterName, String para
45
72
* @param parameterType the expected type of the missing parameter
46
73
* @param missingAfterConversion whether the value became null after conversion
47
74
* @since 5.3.6
75
+ * @deprecated in favor of {@link #MissingServletRequestParameterException(String, MethodParameter, boolean)}
48
76
*/
77
+ @ Deprecated (since = "6.1" , forRemoval = true )
49
78
public MissingServletRequestParameterException (
50
79
String parameterName , String parameterType , boolean missingAfterConversion ) {
51
80
52
81
super ("" , missingAfterConversion , null , new Object [] {parameterName });
53
82
this .parameterName = parameterName ;
54
83
this .parameterType = parameterType ;
55
- getBody ().setDetail ("Required parameter '" + this .parameterName + "' is not present." );
84
+ this .parameter = null ;
85
+ getBody ().setDetail (initBodyDetail (this .parameterName ));
86
+ }
87
+
88
+ private static String initBodyDetail (String name ) {
89
+ return "Required parameter '" + name + "' is not present." ;
56
90
}
57
91
58
92
@@ -77,4 +111,14 @@ public final String getParameterType() {
77
111
return this .parameterType ;
78
112
}
79
113
114
+ /**
115
+ * Return the target {@link MethodParameter} if the exception was raised for
116
+ * a controller method argument.
117
+ * @since 6.1
118
+ */
119
+ @ Nullable
120
+ public MethodParameter getMethodParameter () {
121
+ return this .parameter ;
122
+ }
123
+
80
124
}
0 commit comments