Skip to content

Commit 6f5359e

Browse files
committed
Enrich TypeMismatchException for controller method args
Issue: SPR-10153
1 parent 13403d5 commit 6f5359e

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -20,6 +20,8 @@
2020
import java.util.concurrent.ConcurrentHashMap;
2121
import javax.servlet.ServletException;
2222

23+
import org.springframework.beans.ConversionNotSupportedException;
24+
import org.springframework.beans.TypeMismatchException;
2325
import org.springframework.beans.factory.config.BeanExpressionContext;
2426
import org.springframework.beans.factory.config.BeanExpressionResolver;
2527
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@@ -101,7 +103,18 @@ else if ("".equals(arg) && namedValueInfo.defaultValue != null) {
101103

102104
if (binderFactory != null) {
103105
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
104-
arg = binder.convertIfNecessary(arg, paramType, parameter);
106+
try {
107+
arg = binder.convertIfNecessary(arg, paramType, parameter);
108+
}
109+
catch (ConversionNotSupportedException ex) {
110+
throw new MethodArgumentConversionNotSupportedException(arg, ex.getRequiredType(),
111+
ex.getCause(), namedValueInfo.name, parameter);
112+
}
113+
catch (TypeMismatchException ex) {
114+
throw new MethodArgumentTypeMismatchException(arg, ex.getRequiredType(),
115+
ex.getCause(), namedValueInfo.name, parameter);
116+
117+
}
105118
}
106119

107120
handleResolvedValue(arg, namedValueInfo.name, parameter, mavContainer, webRequest);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2002-2015 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+
package org.springframework.web.method.annotation;
17+
18+
import org.springframework.beans.ConversionNotSupportedException;
19+
import org.springframework.core.MethodParameter;
20+
21+
/**
22+
* A ConversionNotSupportedException raised while resolving a method argument.
23+
* Provides access to the target {@link org.springframework.core.MethodParameter
24+
* MethodParameter}.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 4.2
28+
*/
29+
@SuppressWarnings("serial")
30+
public class MethodArgumentConversionNotSupportedException extends ConversionNotSupportedException {
31+
32+
private final String name;
33+
34+
private final MethodParameter parameter;
35+
36+
37+
public MethodArgumentConversionNotSupportedException(Object value, Class<?> requiredType,
38+
Throwable cause, String name, MethodParameter param) {
39+
40+
super(value, requiredType, cause);
41+
this.name = name;
42+
this.parameter = param;
43+
}
44+
45+
46+
/**
47+
* Return the name of the method argument.
48+
*/
49+
public String getName() {
50+
return this.name;
51+
}
52+
53+
/**
54+
* Return the target method parameter.
55+
*/
56+
public MethodParameter getParameter() {
57+
return this.parameter;
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2002-2015 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+
package org.springframework.web.method.annotation;
17+
18+
import org.springframework.beans.TypeMismatchException;
19+
import org.springframework.core.MethodParameter;
20+
21+
/**
22+
* A TypeMismatchException raised while resolving a controller method argument.
23+
* Provides access to the target {@link org.springframework.core.MethodParameter
24+
* MethodParameter}.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 4.2
28+
*/
29+
@SuppressWarnings("serial")
30+
public class MethodArgumentTypeMismatchException extends TypeMismatchException {
31+
32+
private final String name;
33+
34+
private final MethodParameter parameter;
35+
36+
37+
public MethodArgumentTypeMismatchException(Object value, Class<?> requiredType,
38+
Throwable cause, String name, MethodParameter param) {
39+
40+
super(value, requiredType, cause);
41+
this.name = name;
42+
this.parameter = param;
43+
}
44+
45+
46+
/**
47+
* Return the name of the method argument.
48+
*/
49+
public String getName() {
50+
return this.name;
51+
}
52+
53+
/**
54+
* Return the target method parameter.
55+
*/
56+
public MethodParameter getParameter() {
57+
return this.parameter;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)