diff --git a/spring-web/src/main/java/org/springframework/web/bind/UnsatisfiedServletRequestParameterException.java b/spring-web/src/main/java/org/springframework/web/bind/UnsatisfiedServletRequestParameterException.java index c600059cc8..6ecfba7718 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/UnsatisfiedServletRequestParameterException.java +++ b/spring-web/src/main/java/org/springframework/web/bind/UnsatisfiedServletRequestParameterException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 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. @@ -16,9 +16,13 @@ package org.springframework.web.bind; +import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.Map; +import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; @@ -34,7 +38,7 @@ import org.springframework.util.StringUtils; @SuppressWarnings("serial") public class UnsatisfiedServletRequestParameterException extends ServletRequestBindingException { - private final String[] paramConditions; + private final List paramConditions; private final Map actualParams; @@ -46,6 +50,21 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB */ public UnsatisfiedServletRequestParameterException(String[] paramConditions, Map actualParams) { super(""); + this.paramConditions = Arrays.asList(paramConditions); + this.actualParams = actualParams; + } + + /** + * Create a new UnsatisfiedServletRequestParameterException. + * @param paramConditions all sets of parameter conditions that have been violated + * @param actualParams the actual parameter Map associated with the ServletRequest + * @since 4.2 + */ + public UnsatisfiedServletRequestParameterException(List paramConditions, + Map actualParams) { + + super(""); + Assert.isTrue(!CollectionUtils.isEmpty(paramConditions)); this.paramConditions = paramConditions; this.actualParams = actualParams; } @@ -53,8 +72,20 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB @Override public String getMessage() { - return "Parameter conditions \"" + StringUtils.arrayToDelimitedString(this.paramConditions, ", ") + - "\" not met for actual request parameters: " + requestParameterMapToString(this.actualParams); + StringBuilder sb = new StringBuilder("Parameter conditions "); + int i = 0; + for (String[] conditions : this.paramConditions) { + if (i > 0) { + sb.append(" OR "); + } + sb.append("\""); + sb.append(StringUtils.arrayToDelimitedString(conditions, ", ")); + sb.append("\""); + i++; + } + sb.append(" not met for actual request parameters: "); + sb.append(requestParameterMapToString(this.actualParams)); + return sb.toString(); } private static String requestParameterMapToString(Map actualParams) { @@ -70,10 +101,20 @@ public class UnsatisfiedServletRequestParameterException extends ServletRequestB } /** - * Return the parameter conditions that have been violated. + * Return the parameter conditions that have been violated or the first group + * in case of multiple groups. * @see org.springframework.web.bind.annotation.RequestMapping#params() */ public final String[] getParamConditions() { + return this.paramConditions.get(0); + } + + /** + * Return all parameter condition groups that have been violated. + * @see org.springframework.web.bind.annotation.RequestMapping#params() + * @since 4.2 + */ + public final List getParamConditionGroups() { return this.paramConditions; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java index 5574ccfebc..5c18540afe 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -22,6 +22,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -205,7 +206,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe Set consumableMediaTypes; Set producibleMediaTypes; - Set paramConditions; + List paramConditions; if (patternAndMethodMatches.isEmpty()) { consumableMediaTypes = getConsumableMediaTypes(request, patternMatches); @@ -234,8 +235,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe throw new HttpMediaTypeNotAcceptableException(new ArrayList(producibleMediaTypes)); } else if (!CollectionUtils.isEmpty(paramConditions)) { - String[] params = paramConditions.toArray(new String[paramConditions.size()]); - throw new UnsatisfiedServletRequestParameterException(params, request.getParameterMap()); + throw new UnsatisfiedServletRequestParameterException(paramConditions, request.getParameterMap()); } else { return null; @@ -262,18 +262,21 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe return result; } - private Set getRequestParams(HttpServletRequest request, Set partialMatches) { + private List getRequestParams(HttpServletRequest request, Set partialMatches) { + List result = new ArrayList(); for (RequestMappingInfo partialMatch : partialMatches) { ParamsRequestCondition condition = partialMatch.getParamsCondition(); - if (!CollectionUtils.isEmpty(condition.getExpressions()) && (condition.getMatchingCondition(request) == null)) { - Set expressions = new HashSet(); - for (NameValueExpression expr : condition.getExpressions()) { - expressions.add(expr.toString()); + Set> expressions = condition.getExpressions(); + if (!CollectionUtils.isEmpty(expressions) && condition.getMatchingCondition(request) == null) { + int i = 0; + String[] array = new String[expressions.size()]; + for (NameValueExpression expression : expressions) { + array[i++] = expression.toString(); } - return expressions; + result.add(array); } } - return null; + return result; } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java index b9c893a4c2..cdbae443f5 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -16,12 +16,17 @@ package org.springframework.web.servlet.mvc.method; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; + import javax.servlet.http.HttpServletRequest; import org.junit.Before; @@ -54,8 +59,6 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition; import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition; import org.springframework.web.util.UrlPathHelper; -import static org.junit.Assert.*; - /** * Test fixture with {@link RequestMappingInfoHandlerMapping}. * @@ -211,6 +214,8 @@ public class RequestMappingInfoHandlerMappingTests { } } + // SPR-12854 + @Test public void testUnsatisfiedServletRequestParameterException() throws Exception { try { @@ -219,8 +224,10 @@ public class RequestMappingInfoHandlerMappingTests { fail("UnsatisfiedServletRequestParameterException expected"); } catch (UnsatisfiedServletRequestParameterException ex) { - assertArrayEquals("Invalid request parameter conditions", - new String[] { "foo=bar" }, ex.getParamConditions()); + List groups = ex.getParamConditionGroups(); + assertEquals(2, groups.size()); + assertThat(Arrays.asList("foo=bar", "bar=baz"), + containsInAnyOrder(groups.get(0)[0], groups.get(1)[0])); } } @@ -408,6 +415,7 @@ public class RequestMappingInfoHandlerMappingTests { } + @SuppressWarnings("unused") @Controller private static class TestController { @@ -441,6 +449,11 @@ public class RequestMappingInfoHandlerMappingTests { return ""; } + @RequestMapping(value = "/params", params="bar=baz") + public String param2() { + return ""; + } + @RequestMapping(value = "/content", produces="application/xml") public String xmlContent() { return ""; @@ -452,6 +465,7 @@ public class RequestMappingInfoHandlerMappingTests { } } + @SuppressWarnings("unused") @Controller private static class UserController {