Skip to content

Commit 1e3161b

Browse files
committed
Expose MethodParameter in MissingServletRequestParameterException
See gh-26219
1 parent 6b50b7b commit 1e3161b

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.web.bind;
1818

19+
import org.springframework.core.MethodParameter;
20+
import org.springframework.lang.Nullable;
21+
1922
/**
2023
* {@link ServletRequestBindingException} subclass that indicates a missing parameter.
2124
*
@@ -29,14 +32,38 @@ public class MissingServletRequestParameterException extends MissingRequestValue
2932

3033
private final String parameterType;
3134

35+
@Nullable
36+
private final MethodParameter parameter;
37+
3238

3339
/**
3440
* Constructor for MissingServletRequestParameterException.
3541
* @param parameterName the name of the missing parameter
3642
* @param parameterType the expected type of the missing parameter
3743
*/
3844
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));
4067
}
4168

4269
/**
@@ -45,14 +72,21 @@ public MissingServletRequestParameterException(String parameterName, String para
4572
* @param parameterType the expected type of the missing parameter
4673
* @param missingAfterConversion whether the value became null after conversion
4774
* @since 5.3.6
75+
* @deprecated in favor of {@link #MissingServletRequestParameterException(String, MethodParameter, boolean)}
4876
*/
77+
@Deprecated(since = "6.1", forRemoval = true)
4978
public MissingServletRequestParameterException(
5079
String parameterName, String parameterType, boolean missingAfterConversion) {
5180

5281
super("", missingAfterConversion, null, new Object[] {parameterName});
5382
this.parameterName = parameterName;
5483
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.";
5690
}
5791

5892

@@ -77,4 +111,14 @@ public final String getParameterType() {
77111
return this.parameterType;
78112
}
79113

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+
80124
}

spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -214,8 +214,7 @@ protected void handleMissingValueInternal(
214214
}
215215
}
216216
else {
217-
throw new MissingServletRequestParameterException(name,
218-
parameter.getNestedParameterType().getSimpleName(), missingAfterConversion);
217+
throw new MissingServletRequestParameterException(name, parameter, missingAfterConversion);
219218
}
220219
}
221220

0 commit comments

Comments
 (0)