Consistent support for java.util.Optional for all applicable handler method arguments

Issue: SPR-12171
This commit is contained in:
Juergen Hoeller
2014-09-10 01:27:46 +02:00
parent 559e81bec7
commit 5790fc904a
8 changed files with 83 additions and 51 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -60,10 +60,9 @@ public abstract class AbstractCookieValueMethodArgumentResolver extends Abstract
}
@Override
protected void handleMissingValue(String cookieName, MethodParameter param) throws ServletRequestBindingException {
String paramType = param.getParameterType().getName();
protected void handleMissingValue(String cookieName, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException(
"Missing cookie named '" + cookieName + "' for method parameter type [" + paramType + "]");
"Missing cookie named '" + cookieName + "' for method parameter type " + parameter.getParameterType().getSimpleName());
}
private static class CookieValueNamedValueInfo extends NamedValueInfo {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -91,7 +91,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
if (namedValueInfo.defaultValue != null) {
arg = resolveDefaultValue(namedValueInfo.defaultValue);
}
else if (namedValueInfo.required) {
else if (namedValueInfo.required && !parameter.getParameterType().getName().equals("java.util.Optional")) {
handleMissingValue(namedValueInfo.name, parameter);
}
arg = handleNullValue(namedValueInfo.name, arg, paramType);
@@ -138,8 +138,10 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
String name = info.name;
if (info.name.length() == 0) {
name = parameter.getParameterName();
Assert.notNull(name, "Name for argument type [" + parameter.getParameterType().getName()
+ "] not available, and parameter name information not found in class file either.");
if (name == null) {
throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() +
"] not available, and parameter name information not found in class file either.");
}
}
String defaultValue = (ValueConstants.DEFAULT_NONE.equals(info.defaultValue) ? null : info.defaultValue);
return new NamedValueInfo(name, info.required, defaultValue);

View File

@@ -76,13 +76,9 @@ public class RequestHeaderMethodArgumentResolver extends AbstractNamedValueMetho
}
@Override
protected void handleMissingValue(String headerName, MethodParameter param) throws ServletRequestBindingException {
Class<?> paramType = param.getParameterType();
if (!paramType.getName().equals("java.util.Optional")) {
throw new ServletRequestBindingException(
"Missing header '" + headerName + "' for method parameter type [" + paramType.getName() + "]");
}
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException {
throw new ServletRequestBindingException("Missing header '" + name +
"' for method parameter type " + parameter.getParameterType().getSimpleName());
}
private static class RequestHeaderNamedValueInfo extends NamedValueInfo {

View File

@@ -250,10 +250,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
@Override
protected void handleMissingValue(String paramName, MethodParameter parameter) throws ServletException {
if (!parameter.getParameterType().getName().equals("java.util.Optional")) {
throw new MissingServletRequestParameterException(paramName, parameter.getParameterType().getSimpleName());
}
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException {
throw new MissingServletRequestParameterException(name, parameter.getParameterType().getSimpleName());
}
@Override