diff --git a/spring-core/src/main/java/org/springframework/util/MultiValueMap.java b/spring-core/src/main/java/org/springframework/util/MultiValueMap.java index a526af78ab..408273db57 100644 --- a/spring-core/src/main/java/org/springframework/util/MultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/MultiValueMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2018 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. @@ -30,7 +30,7 @@ public interface MultiValueMap extends Map> { /** * Return the first value for the given key. * @param key the key - * @return the first value for the specified key, or {@code null} + * @return the first value for the specified key, or {@code null} if none */ V getFirst(K key); @@ -55,7 +55,7 @@ public interface MultiValueMap extends Map> { void setAll(Map values); /** - * Returns the first values contained in this {@code MultiValueMap}. + * Return a {@code Map} with the first values contained in this {@code MultiValueMap}. * @return a single value representation of this map */ Map toSingleValueMap(); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java index 0d0a341503..ce00d3ad7a 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java @@ -118,7 +118,7 @@ public class InvocableHandlerMethod extends HandlerMethod { } /** - * Get the method argument values for the current request. + * Get the method argument values for the current message. */ private Object[] getMethodArgumentValues(Message message, Object... providedArgs) throws Exception { MethodParameter[] parameters = getMethodParameters(); diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java index 8f27d5c86f..191053cb4e 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -33,23 +33,24 @@ import org.springframework.web.multipart.MultipartResolver; * Annotation that can be used to associate the part of a "multipart/form-data" request * with a method argument. * - *

Supported method argument types include {@link MultipartFile} - * in conjunction with Spring's {@link MultipartResolver} abstraction, - * {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests, - * or otherwise for any other method argument, the content of the part is passed through an - * {@link HttpMessageConverter} taking into consideration the 'Content-Type' header - * of the request part. This is analogous to what @{@link RequestBody} does to resolve - * an argument based on the content of a non-multipart regular request. + *

Supported method argument types include {@link MultipartFile} in conjunction with + * Spring's {@link MultipartResolver} abstraction, {@code javax.servlet.http.Part} in + * conjunction with Servlet 3.0 multipart requests, or otherwise for any other method + * argument, the content of the part is passed through an {@link HttpMessageConverter} + * taking into consideration the 'Content-Type' header of the request part. This is + * analogous to what @{@link RequestBody} does to resolve an argument based on the + * content of a non-multipart regular request. * - *

Note that @{@link RequestParam} annotation can also be used to associate the - * part of a "multipart/form-data" request with a method argument supporting the same - * method argument types. The main difference is that when the method argument is not a - * String, @{@link RequestParam} relies on type conversion via a registered - * {@link Converter} or {@link PropertyEditor} while @{@link RequestPart} relies - * on {@link HttpMessageConverter}s taking into consideration the 'Content-Type' header - * of the request part. @{@link RequestParam} is likely to be used with name-value form - * fields while @{@link RequestPart} is likely to be used with parts containing more - * complex content (e.g. JSON, XML). + *

Note that @{@link RequestParam} annotation can also be used to associate the part + * of a "multipart/form-data" request with a method argument supporting the same method + * argument types. The main difference is that when the method argument is not a String + * or raw {@code MultipartFile} / {@code Part}, {@code @RequestParam} relies on type + * conversion via a registered {@link Converter} or {@link PropertyEditor} while + * {@link RequestPart} relies on {@link HttpMessageConverter HttpMessageConverters} + * taking into consideration the 'Content-Type' header of the request part. + * {@link RequestParam} is likely to be used with name-value form fields while + * {@link RequestPart} is likely to be used with parts containing more complex content + * e.g. JSON, XML). * * @author Rossen Stoyanchev * @author Arjen Poutsma diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java b/spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java index 07effd2c62..30f41dfcda 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 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. @@ -56,7 +56,7 @@ public class MapMethodProcessor implements HandlerMethodArgumentResolver, Handle } @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java index 39fce25801..2471d0349a 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -37,7 +37,7 @@ import org.springframework.web.method.support.ModelAndViewContainer; * *

The created {@link Map} contains all request parameter name/value pairs. * If the method parameter type is {@link MultiValueMap} instead, the created - * map contains all request parameters and all there values for cases where + * map contains all request parameters and all their values for cases where * request parameters have multiple values. * * @author Arjen Poutsma @@ -50,22 +50,16 @@ public class RequestParamMapMethodArgumentResolver implements HandlerMethodArgum @Override public boolean supportsParameter(MethodParameter parameter) { RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class); - if (requestParam != null) { - if (Map.class.isAssignableFrom(parameter.getParameterType())) { - return !StringUtils.hasText(requestParam.name()); - } - } - return false; + return (requestParam != null && Map.class.isAssignableFrom(parameter.getParameterType()) && + !StringUtils.hasText(requestParam.name())); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { - Class paramType = parameter.getParameterType(); - Map parameterMap = webRequest.getParameterMap(); - if (MultiValueMap.class.isAssignableFrom(paramType)) { + if (MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) { MultiValueMap result = new LinkedMultiValueMap(parameterMap.size()); for (Map.Entry entry : parameterMap.entrySet()) { for (String value : entry.getValue()) { @@ -84,4 +78,5 @@ public class RequestParamMapMethodArgumentResolver implements HandlerMethodArgum return result; } } + } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java index 50335548a6..55c3ba2611 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -80,6 +80,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod /** + * Create a new {@link RequestParamMethodArgumentResolver} instance. * @param useDefaultResolution in default resolution mode a method argument * that is a simple type, as defined in {@link BeanUtils#isSimpleProperty}, * is treated as a request parameter even if it isn't annotated, the @@ -90,6 +91,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod } /** + * Create a new {@link RequestParamMethodArgumentResolver} instance. * @param beanFactory a bean factory used for resolving ${...} placeholder * and #{...} SpEL expressions in default values, or {@code null} if default * values are not expected to contain expressions @@ -108,15 +110,11 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod * Supports the following: *

    *
  • @RequestParam-annotated method arguments. - * This excludes {@link Map} params where the annotation doesn't - * specify a name. See {@link RequestParamMapMethodArgumentResolver} - * instead for such params. - *
  • Arguments of type {@link MultipartFile} - * unless annotated with @{@link RequestPart}. - *
  • Arguments of type {@code javax.servlet.http.Part} - * unless annotated with @{@link RequestPart}. - *
  • In default resolution mode, simple type arguments - * even if not with @{@link RequestParam}. + * This excludes {@link Map} params where the annotation does not specify a name. + * See {@link RequestParamMapMethodArgumentResolver} instead for such params. + *
  • Arguments of type {@link MultipartFile} unless annotated with @{@link RequestPart}. + *
  • Arguments of type {@code Part} unless annotated with @{@link RequestPart}. + *
  • In default resolution mode, simple type arguments even if not with @{@link RequestParam}. *
*/ @Override diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java index ef2a3f0183..8b8bddd3a2 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -104,7 +104,7 @@ public class RequestHeaderMapMethodArgumentResolverTests { request.addHeader(name, value1); request.addHeader(name, value2); - MultiValueMap expected = new LinkedMultiValueMap(1); + MultiValueMap expected = new LinkedMultiValueMap<>(1); expected.add(name, value1); expected.add(name, value2); @@ -135,9 +135,8 @@ public class RequestHeaderMapMethodArgumentResolverTests { public void params(@RequestHeader Map param1, - @RequestHeader MultiValueMap param2, - @RequestHeader HttpHeaders param3, - Map unsupported) { + @RequestHeader MultiValueMap param2, @RequestHeader HttpHeaders param3, + Map unsupported) { } } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java index a453abb0de..db2e766cde 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java @@ -44,7 +44,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext; import static org.junit.Assert.*; /** - * Test fixture with {@link org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver}. + * Test fixture with {@link RequestHeaderMethodArgumentResolver}. * * @author Arjen Poutsma * @author Rossen Stoyanchev @@ -70,7 +70,7 @@ public class RequestHeaderMethodArgumentResolverTests { @Before @SuppressWarnings("resource") - public void setUp() throws Exception { + public void setup() throws Exception { GenericWebApplicationContext context = new GenericWebApplicationContext(); context.refresh(); resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory()); @@ -94,7 +94,7 @@ public class RequestHeaderMethodArgumentResolverTests { } @After - public void teardown() { + public void reset() { RequestContextHolder.resetRequestAttributes(); } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolverTests.java index cae73f460d..757dbe95db 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMapMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. @@ -59,10 +59,10 @@ public class RequestParamMapMethodArgumentResolverTests { @Before - public void setUp() throws Exception { + public void setup() throws Exception { resolver = new RequestParamMapMethodArgumentResolver(); - Method method = getClass().getMethod("params", Map.class, MultiValueMap.class, Map.class, Map.class); + Method method = getClass().getMethod("handle", Map.class, MultiValueMap.class, Map.class, Map.class); paramMap = new SynthesizingMethodParameter(method, 0); paramMultiValueMap = new SynthesizingMethodParameter(method, 1); paramNamedMap = new SynthesizingMethodParameter(method, 2); @@ -99,9 +99,9 @@ public class RequestParamMapMethodArgumentResolverTests { String name = "foo"; String value1 = "bar"; String value2 = "baz"; - request.addParameter(name, new String[]{value1, value2}); + request.addParameter(name, value1, value2); - MultiValueMap expected = new LinkedMultiValueMap(1); + MultiValueMap expected = new LinkedMultiValueMap<>(1); expected.add(name, value1); expected.add(name, value2); @@ -112,10 +112,11 @@ public class RequestParamMapMethodArgumentResolverTests { } - public void params(@RequestParam Map param1, - @RequestParam MultiValueMap param2, - @RequestParam("name") Map param3, - Map param4) { + public void handle( + @RequestParam Map param1, + @RequestParam MultiValueMap param2, + @RequestParam("name") Map param3, + Map param4) { } } diff --git a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java index 881e1d28c1..9d88a2467e 100644 --- a/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -94,7 +94,7 @@ public class RequestParamMethodArgumentResolverTests { @Before - public void setUp() throws Exception { + public void setup() throws Exception { resolver = new RequestParamMethodArgumentResolver(null, true); ParameterNameDiscoverer paramNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class[]) null); @@ -369,7 +369,7 @@ public class RequestParamMethodArgumentResolverTests { WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder); - this.request.addParameter("stringNotAnnot", ""); + request.addParameter("stringNotAnnot", ""); Object arg = resolver.resolveArgument(paramStringNotAnnot, null, webRequest, binderFactory); assertNull(arg); @@ -383,7 +383,7 @@ public class RequestParamMethodArgumentResolverTests { WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder); - this.request.addParameter("name", ""); + request.addParameter("name", ""); Object arg = resolver.resolveArgument(paramNotRequired, null, webRequest, binderFactory); assertNull(arg); @@ -406,21 +406,21 @@ public class RequestParamMethodArgumentResolverTests { @Test // SPR-10180 public void resolveEmptyValueToDefault() throws Exception { - this.request.addParameter("name", ""); + request.addParameter("name", ""); Object result = resolver.resolveArgument(paramNamedDefaultValueString, null, webRequest, null); assertEquals("bar", result); } @Test public void resolveEmptyValueWithoutDefault() throws Exception { - this.request.addParameter("stringNotAnnot", ""); + request.addParameter("stringNotAnnot", ""); Object result = resolver.resolveArgument(paramStringNotAnnot, null, webRequest, null); assertEquals("", result); } @Test public void resolveEmptyValueRequiredWithoutDefault() throws Exception { - this.request.addParameter("name", ""); + request.addParameter("name", ""); Object result = resolver.resolveArgument(paramRequired, null, webRequest, null); assertEquals("", result); } @@ -435,7 +435,7 @@ public class RequestParamMethodArgumentResolverTests { Object result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); - this.request.addParameter("name", "123"); + request.addParameter("name", "123"); result = resolver.resolveArgument(paramOptional, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(123, ((Optional) result).get()); @@ -466,7 +466,7 @@ public class RequestParamMethodArgumentResolverTests { Object result = resolver.resolveArgument(paramOptionalArray, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); - this.request.addParameter("name", "123", "456"); + request.addParameter("name", "123", "456"); result = resolver.resolveArgument(paramOptionalArray, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertArrayEquals(new Integer[] {123, 456}, (Integer[]) ((Optional) result).get()); @@ -497,7 +497,7 @@ public class RequestParamMethodArgumentResolverTests { Object result = resolver.resolveArgument(paramOptionalList, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); - this.request.addParameter("name", "123", "456"); + request.addParameter("name", "123", "456"); result = resolver.resolveArgument(paramOptionalList, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(Arrays.asList("123", "456"), ((Optional) result).get()); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java index 80ee38523b..cca63932f3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java @@ -72,8 +72,7 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes } } else if (ex.getCause() instanceof Exception) { - ex = (Exception) ex.getCause(); - return doResolveException(request, response, handler, ex); + return doResolveException(request, response, handler, (Exception) ex.getCause()); } return null; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java index bddd91907d..941e47bd79 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -41,7 +41,7 @@ import org.springframework.web.multipart.support.RequestPartServletServerHttpReq /** * Resolves the following method arguments: *
    - *
  • Annotated with {@code @RequestPart} + *
  • Annotated with @{@link RequestPart} *
  • Of type {@link MultipartFile} in conjunction with Spring's {@link MultipartResolver} abstraction *
  • Of type {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests *
@@ -85,11 +85,13 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM /** - * Supports the following: + * Whether the given {@linkplain MethodParameter method parameter} is a multi-part + * supported. Supports the following: *
    *
  • annotated with {@code @RequestPart} *
  • of type {@link MultipartFile} unless annotated with {@code @RequestParam} - *
  • of type {@code javax.servlet.http.Part} unless annotated with {@code @RequestParam} + *
  • of type {@code javax.servlet.http.Part} unless annotated with + * {@code @RequestParam} *
*/ @Override