optional boolean parameters in MVC handler methods resolve to "false" if not present
This commit is contained in:
parent
721f2ca63e
commit
6b2d4a81a0
|
|
@ -384,7 +384,7 @@ public class HandlerMethodInvoker {
|
||||||
else if (required) {
|
else if (required) {
|
||||||
raiseMissingParameterException(paramName, paramType);
|
raiseMissingParameterException(paramName, paramType);
|
||||||
}
|
}
|
||||||
checkValue(paramName, paramValue, paramType);
|
paramValue = checkValue(paramName, paramValue, paramType);
|
||||||
}
|
}
|
||||||
WebDataBinder binder = createBinder(webRequest, null, paramName);
|
WebDataBinder binder = createBinder(webRequest, null, paramName);
|
||||||
initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
|
initBinder(handlerForInitBinderCall, paramName, binder, webRequest);
|
||||||
|
|
@ -411,7 +411,7 @@ public class HandlerMethodInvoker {
|
||||||
else if (required) {
|
else if (required) {
|
||||||
raiseMissingHeaderException(headerName, paramType);
|
raiseMissingHeaderException(headerName, paramType);
|
||||||
}
|
}
|
||||||
checkValue(headerName, headerValue, paramType);
|
headerValue = checkValue(headerName, headerValue, paramType);
|
||||||
}
|
}
|
||||||
WebDataBinder binder = createBinder(webRequest, null, headerName);
|
WebDataBinder binder = createBinder(webRequest, null, headerName);
|
||||||
initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
|
initBinder(handlerForInitBinderCall, headerName, binder, webRequest);
|
||||||
|
|
@ -434,7 +434,7 @@ public class HandlerMethodInvoker {
|
||||||
else if (required) {
|
else if (required) {
|
||||||
raiseMissingCookieException(cookieName, paramType);
|
raiseMissingCookieException(cookieName, paramType);
|
||||||
}
|
}
|
||||||
checkValue(cookieName, cookieValue, paramType);
|
cookieValue = checkValue(cookieName, cookieValue, paramType);
|
||||||
}
|
}
|
||||||
WebDataBinder binder = createBinder(webRequest, null, cookieName);
|
WebDataBinder binder = createBinder(webRequest, null, cookieName);
|
||||||
initBinder(handlerForInitBinderCall, cookieName, binder, webRequest);
|
initBinder(handlerForInitBinderCall, cookieName, binder, webRequest);
|
||||||
|
|
@ -484,12 +484,18 @@ public class HandlerMethodInvoker {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkValue(String name, Object value, Class paramType) {
|
private Object checkValue(String name, Object value, Class paramType) {
|
||||||
if (value == null && paramType.isPrimitive()) {
|
if (value == null) {
|
||||||
throw new IllegalStateException("Optional " + paramType + " parameter '" + name +
|
if (boolean.class.equals(paramType)) {
|
||||||
"' is not present but cannot be translated into a null value due to being declared as a " +
|
return Boolean.FALSE;
|
||||||
"primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
|
}
|
||||||
|
else if (paramType.isPrimitive()) {
|
||||||
|
throw new IllegalStateException("Optional " + paramType + " parameter '" + name +
|
||||||
|
"' is not present but cannot be translated into a null value due to being declared as a " +
|
||||||
|
"primitive type. Consider declaring it as object wrapper for the corresponding primitive type.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
|
private WebDataBinder resolveModelAttribute(String attrName, MethodParameter methodParam,
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,28 @@ public class ServletAnnotationControllerTests {
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void optionalParamPresent() throws Exception {
|
||||||
|
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
|
||||||
|
@Override
|
||||||
|
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
|
||||||
|
GenericWebApplicationContext wac = new GenericWebApplicationContext();
|
||||||
|
wac.registerBeanDefinition("controller", new RootBeanDefinition(OptionalParamController.class));
|
||||||
|
wac.refresh();
|
||||||
|
return wac;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
servlet.init(new MockServletConfig());
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
|
||||||
|
request.addParameter("id", "val");
|
||||||
|
request.addParameter("flag", "true");
|
||||||
|
request.addHeader("header", "otherVal");
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals("val-true-otherVal", response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void optionalParamMissing() throws Exception {
|
public void optionalParamMissing() throws Exception {
|
||||||
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
|
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
|
||||||
|
|
@ -146,7 +168,7 @@ public class ServletAnnotationControllerTests {
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
assertEquals("null-null", response.getContentAsString());
|
assertEquals("null-false-null", response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -1318,10 +1340,10 @@ public class ServletAnnotationControllerTests {
|
||||||
public static class OptionalParamController {
|
public static class OptionalParamController {
|
||||||
|
|
||||||
@RequestMapping("/myPath.do")
|
@RequestMapping("/myPath.do")
|
||||||
public void myHandle(@RequestParam(required = false) String id,
|
public void myHandle(@RequestParam(required = false) String id, @RequestParam(required = false) boolean flag,
|
||||||
@RequestHeader(value = "header", required = false) String header, HttpServletResponse response)
|
@RequestHeader(value = "header", required = false) String header, HttpServletResponse response)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
response.getWriter().write(String.valueOf(id) + "-" + String.valueOf(header));
|
response.getWriter().write(String.valueOf(id) + "-" + flag + "-" + String.valueOf(header));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue