28
28
import javax .servlet .http .HttpServletRequest ;
29
29
30
30
import org .springframework .http .MediaType ;
31
+ import org .springframework .util .CollectionUtils ;
31
32
import org .springframework .util .MultiValueMap ;
32
33
import org .springframework .util .StringUtils ;
33
34
import org .springframework .web .HttpMediaTypeNotAcceptableException ;
34
35
import org .springframework .web .HttpMediaTypeNotSupportedException ;
35
36
import org .springframework .web .HttpRequestMethodNotSupportedException ;
37
+ import org .springframework .web .bind .UnsatisfiedServletRequestParameterException ;
36
38
import org .springframework .web .bind .annotation .RequestMethod ;
37
39
import org .springframework .web .method .HandlerMethod ;
38
40
import org .springframework .web .servlet .HandlerMapping ;
39
41
import org .springframework .web .servlet .handler .AbstractHandlerMethodMapping ;
42
+ import org .springframework .web .servlet .mvc .condition .NameValueExpression ;
43
+ import org .springframework .web .servlet .mvc .condition .ParamsRequestCondition ;
40
44
import org .springframework .web .util .WebUtils ;
41
45
42
46
/**
@@ -185,14 +189,17 @@ else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
185
189
186
190
Set <MediaType > consumableMediaTypes ;
187
191
Set <MediaType > producibleMediaTypes ;
192
+ Set <String > paramConditions ;
188
193
189
194
if (patternAndMethodMatches .isEmpty ()) {
190
195
consumableMediaTypes = getConsumableMediaTypes (request , patternMatches );
191
196
producibleMediaTypes = getProdicubleMediaTypes (request , patternMatches );
197
+ paramConditions = getRequestParams (request , patternMatches );
192
198
}
193
199
else {
194
200
consumableMediaTypes = getConsumableMediaTypes (request , patternAndMethodMatches );
195
201
producibleMediaTypes = getProdicubleMediaTypes (request , patternAndMethodMatches );
202
+ paramConditions = getRequestParams (request , patternAndMethodMatches );
196
203
}
197
204
198
205
if (!consumableMediaTypes .isEmpty ()) {
@@ -205,6 +212,10 @@ else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
205
212
else if (!producibleMediaTypes .isEmpty ()) {
206
213
throw new HttpMediaTypeNotAcceptableException (new ArrayList <MediaType >(producibleMediaTypes ));
207
214
}
215
+ else if (!CollectionUtils .isEmpty (paramConditions )) {
216
+ String [] params = paramConditions .toArray (new String [paramConditions .size ()]);
217
+ throw new UnsatisfiedServletRequestParameterException (params , request .getParameterMap ());
218
+ }
208
219
else {
209
220
return null ;
210
221
}
@@ -230,4 +241,18 @@ private Set<MediaType> getProdicubleMediaTypes(HttpServletRequest request, Set<R
230
241
return result ;
231
242
}
232
243
244
+ private Set <String > getRequestParams (HttpServletRequest request , Set <RequestMappingInfo > partialMatches ) {
245
+ for (RequestMappingInfo partialMatch : partialMatches ) {
246
+ ParamsRequestCondition condition = partialMatch .getParamsCondition ();
247
+ if (!CollectionUtils .isEmpty (condition .getExpressions ()) && (condition .getMatchingCondition (request ) == null )) {
248
+ Set <String > expressions = new HashSet <String >();
249
+ for (NameValueExpression expr : condition .getExpressions ()) {
250
+ expressions .add (expr .toString ());
251
+ }
252
+ return expressions ;
253
+ }
254
+ }
255
+ return null ;
256
+ }
257
+
233
258
}
0 commit comments