Skip to content

Commit c77dbbb

Browse files
committed
Specific exception for missing request header, cookie, matrix variable
Issue: SPR-14818
1 parent e8034f2 commit c77dbbb

File tree

7 files changed

+219
-8
lines changed

7 files changed

+219
-8
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.bind;
18+
19+
import org.springframework.core.MethodParameter;
20+
21+
/**
22+
* {@link ServletRequestBindingException} subclass that indicates that a matrix
23+
* variable expected in the method parameters of an {@code @RequestMapping}
24+
* method is not present among the matrix variables extracted from the URL.
25+
*
26+
* @author Juergen Hoeller
27+
* @since 5.1
28+
* @see MissingPathVariableException
29+
*/
30+
@SuppressWarnings("serial")
31+
public class MissingMatrixVariableException extends ServletRequestBindingException {
32+
33+
private final String variableName;
34+
35+
private final MethodParameter parameter;
36+
37+
38+
/**
39+
* Constructor for MissingMatrixVariableException.
40+
* @param variableName the name of the missing matrix variable
41+
* @param parameter the method parameter
42+
*/
43+
public MissingMatrixVariableException(String variableName, MethodParameter parameter) {
44+
super("");
45+
this.variableName = variableName;
46+
this.parameter = parameter;
47+
}
48+
49+
50+
@Override
51+
public String getMessage() {
52+
return "Missing matrix variable '" + this.variableName +
53+
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
54+
}
55+
56+
/**
57+
* Return the expected name of the matrix variable.
58+
*/
59+
public final String getVariableName() {
60+
return this.variableName;
61+
}
62+
63+
/**
64+
* Return the method parameter bound to the matrix variable.
65+
*/
66+
public final MethodParameter getParameter() {
67+
return this.parameter;
68+
}
69+
70+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -27,6 +27,7 @@
2727
*
2828
* @author Rossen Stoyanchev
2929
* @since 4.2
30+
* @see MissingMatrixVariableException
3031
*/
3132
@SuppressWarnings("serial")
3233
public class MissingPathVariableException extends ServletRequestBindingException {
@@ -51,7 +52,7 @@ public MissingPathVariableException(String variableName, MethodParameter paramet
5152
@Override
5253
public String getMessage() {
5354
return "Missing URI template variable '" + this.variableName +
54-
"' for method parameter of type " + this.parameter.getParameterType().getSimpleName();
55+
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
5556
}
5657

5758
/**
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.bind;
18+
19+
import org.springframework.core.MethodParameter;
20+
21+
/**
22+
* {@link ServletRequestBindingException} subclass that indicates
23+
* that a request cookie expected in the method parameters of an
24+
* {@code @RequestMapping} method is not present.
25+
*
26+
* @author Juergen Hoeller
27+
* @since 5.1
28+
* @see MissingRequestHeaderException
29+
*/
30+
@SuppressWarnings("serial")
31+
public class MissingRequestCookieException extends ServletRequestBindingException {
32+
33+
private final String cookieName;
34+
35+
private final MethodParameter parameter;
36+
37+
38+
/**
39+
* Constructor for MissingRequestCookieException.
40+
* @param cookieName the name of the missing request cookie
41+
* @param parameter the method parameter
42+
*/
43+
public MissingRequestCookieException(String cookieName, MethodParameter parameter) {
44+
super("");
45+
this.cookieName = cookieName;
46+
this.parameter = parameter;
47+
}
48+
49+
50+
@Override
51+
public String getMessage() {
52+
return "Missing cookie '" + this.cookieName +
53+
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
54+
}
55+
56+
/**
57+
* Return the expected name of the request cookie.
58+
*/
59+
public final String getCookieName() {
60+
return this.cookieName;
61+
}
62+
63+
/**
64+
* Return the method parameter bound to the request cookie.
65+
*/
66+
public final MethodParameter getParameter() {
67+
return this.parameter;
68+
}
69+
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.bind;
18+
19+
import org.springframework.core.MethodParameter;
20+
21+
/**
22+
* {@link ServletRequestBindingException} subclass that indicates
23+
* that a request header expected in the method parameters of an
24+
* {@code @RequestMapping} method is not present.
25+
*
26+
* @author Juergen Hoeller
27+
* @since 5.1
28+
* @see MissingRequestCookieException
29+
*/
30+
@SuppressWarnings("serial")
31+
public class MissingRequestHeaderException extends ServletRequestBindingException {
32+
33+
private final String headerName;
34+
35+
private final MethodParameter parameter;
36+
37+
38+
/**
39+
* Constructor for MissingRequestHeaderException.
40+
* @param headerName the name of the missing request header
41+
* @param parameter the method parameter
42+
*/
43+
public MissingRequestHeaderException(String headerName, MethodParameter parameter) {
44+
super("");
45+
this.headerName = headerName;
46+
this.parameter = parameter;
47+
}
48+
49+
50+
@Override
51+
public String getMessage() {
52+
return "Missing request header '" + this.headerName +
53+
"' for method parameter of type " + this.parameter.getNestedParameterType().getSimpleName();
54+
}
55+
56+
/**
57+
* Return the expected name of the request header.
58+
*/
59+
public final String getHeaderName() {
60+
return this.headerName;
61+
}
62+
63+
/**
64+
* Return the method parameter bound to the request header.
65+
*/
66+
public final MethodParameter getParameter() {
67+
return this.parameter;
68+
}
69+
70+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.core.MethodParameter;
2121
import org.springframework.lang.Nullable;
2222
import org.springframework.util.Assert;
23+
import org.springframework.web.bind.MissingRequestCookieException;
2324
import org.springframework.web.bind.ServletRequestBindingException;
2425
import org.springframework.web.bind.WebDataBinder;
2526
import org.springframework.web.bind.annotation.CookieValue;
@@ -66,8 +67,7 @@ protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) {
6667

6768
@Override
6869
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
69-
throw new ServletRequestBindingException("Missing cookie '" + name +
70-
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
70+
throw new MissingRequestCookieException(name, parameter);
7171
}
7272

7373

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.core.MethodParameter;
2323
import org.springframework.lang.Nullable;
2424
import org.springframework.util.Assert;
25+
import org.springframework.web.bind.MissingRequestHeaderException;
2526
import org.springframework.web.bind.ServletRequestBindingException;
2627
import org.springframework.web.bind.WebDataBinder;
2728
import org.springframework.web.bind.annotation.RequestHeader;
@@ -83,8 +84,7 @@ protected Object resolveName(String name, MethodParameter parameter, NativeWebRe
8384

8485
@Override
8586
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
86-
throw new ServletRequestBindingException("Missing request header '" + name +
87-
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
87+
throw new MissingRequestHeaderException(name, parameter);
8888
}
8989

9090

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MatrixVariableMethodArgumentResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.util.CollectionUtils;
2727
import org.springframework.util.MultiValueMap;
2828
import org.springframework.util.StringUtils;
29+
import org.springframework.web.bind.MissingMatrixVariableException;
2930
import org.springframework.web.bind.ServletRequestBindingException;
3031
import org.springframework.web.bind.annotation.MatrixVariable;
3132
import org.springframework.web.bind.annotation.ValueConstants;
@@ -122,8 +123,7 @@ else if (paramValues.size() == 1) {
122123

123124
@Override
124125
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
125-
throw new ServletRequestBindingException("Missing matrix variable '" + name +
126-
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName());
126+
throw new MissingMatrixVariableException(name, parameter);
127127
}
128128

129129

0 commit comments

Comments
 (0)