From ffa032e78f573bc37652112cbe3ff6e48299ef14 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 24 Oct 2018 20:46:26 +0200 Subject: [PATCH] Polishing --- .../springframework/util/MultiValueMap.java | 2 +- .../web/bind/annotation/RequestPart.java | 33 ++++++++--------- .../method/annotation/MapMethodProcessor.java | 4 +-- .../RequestParamMethodArgumentResolver.java | 18 ++++------ ...tHeaderMapMethodArgumentResolverTests.java | 2 +- ...uestHeaderMethodArgumentResolverTests.java | 6 ++-- ...questParamMethodArgumentResolverTests.java | 35 +++++++------------ .../ResponseStatusExceptionResolver.java | 7 ++-- .../RequestPartMethodArgumentResolver.java | 2 +- 9 files changed, 49 insertions(+), 60 deletions(-) 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 a80676839ed..a4d9d5d2eae 100644 --- a/spring-core/src/main/java/org/springframework/util/MultiValueMap.java +++ b/spring-core/src/main/java/org/springframework/util/MultiValueMap.java @@ -75,7 +75,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-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 c2328137016..191053cb4ef 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 @@ -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 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). + *

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 a21dd236570..8ebee97cbde 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-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. @@ -60,7 +60,7 @@ public class MapMethodProcessor implements HandlerMethodArgumentResolver, Handle } @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { 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 d8ae175b726..f00d22265b7 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 @@ -41,7 +41,7 @@ import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.method.support.UriComponentsContributor; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.multipart.MultipartHttpServletRequest; +import org.springframework.web.multipart.MultipartRequest; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.support.MissingServletRequestPartException; import org.springframework.web.multipart.support.MultipartResolutionDelegate; @@ -114,15 +114,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 @@ -172,7 +168,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod } Object arg = null; - MultipartHttpServletRequest multipartRequest = request.getNativeRequest(MultipartHttpServletRequest.class); + MultipartRequest multipartRequest = request.getNativeRequest(MultipartRequest.class); if (multipartRequest != null) { List files = multipartRequest.getFiles(name); if (!files.isEmpty()) { 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 8b8bddd3a29..b4c7e9a8127 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 @@ -60,7 +60,7 @@ public class RequestHeaderMapMethodArgumentResolverTests { @Before - public void setUp() throws Exception { + public void setup() throws Exception { resolver = new RequestHeaderMapMethodArgumentResolver(); Method method = getClass().getMethod("params", Map.class, MultiValueMap.class, HttpHeaders.class, Map.class); 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 a453abb0dee..db2e766cdec 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/RequestParamMethodArgumentResolverTests.java b/spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java index eb6513e2f65..6b089ef04f9 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-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. @@ -22,7 +22,6 @@ import java.util.Map; import java.util.Optional; import javax.servlet.http.Part; -import org.junit.Before; import org.junit.Test; import org.springframework.beans.propertyeditors.StringTrimmerEditor; @@ -53,7 +52,7 @@ import static org.mockito.BDDMockito.*; import static org.springframework.web.method.MvcAnnotationPredicates.*; /** - * Test fixture with {@link org.springframework.web.method.annotation.RequestParamMethodArgumentResolver}. + * Test fixture with {@link RequestParamMethodArgumentResolver}. * * @author Arjen Poutsma * @author Rossen Stoyanchev @@ -61,23 +60,15 @@ import static org.springframework.web.method.MvcAnnotationPredicates.*; */ public class RequestParamMethodArgumentResolverTests { - private RequestParamMethodArgumentResolver resolver; + private RequestParamMethodArgumentResolver resolver = new RequestParamMethodArgumentResolver(null, true); - private NativeWebRequest webRequest; + private MockHttpServletRequest request = new MockHttpServletRequest(); - private MockHttpServletRequest request; + private NativeWebRequest webRequest = new ServletWebRequest(request, new MockHttpServletResponse()); private ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build(); - @Before - public void setup() throws Exception { - resolver = new RequestParamMethodArgumentResolver(null, true); - request = new MockHttpServletRequest(); - webRequest = new ServletWebRequest(request, new MockHttpServletResponse()); - } - - @Test public void supportsParameter() { resolver = new RequestParamMethodArgumentResolver(null, true); @@ -385,7 +376,7 @@ public class RequestParamMethodArgumentResolverTests { WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder); - this.request.addParameter("stringNotAnnot", ""); + request.addParameter("stringNotAnnot", ""); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory); @@ -400,7 +391,7 @@ public class RequestParamMethodArgumentResolverTests { WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class); given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder); - this.request.addParameter("name", ""); + request.addParameter("name", ""); MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class); Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory); @@ -426,7 +417,7 @@ public class RequestParamMethodArgumentResolverTests { @Test // SPR-10180 public void resolveEmptyValueToDefault() throws Exception { - this.request.addParameter("name", ""); + request.addParameter("name", ""); MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertEquals("bar", result); @@ -434,7 +425,7 @@ public class RequestParamMethodArgumentResolverTests { @Test public void resolveEmptyValueWithoutDefault() throws Exception { - this.request.addParameter("stringNotAnnot", ""); + request.addParameter("stringNotAnnot", ""); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertEquals("", result); @@ -442,7 +433,7 @@ public class RequestParamMethodArgumentResolverTests { @Test public void resolveEmptyValueRequiredWithoutDefault() throws Exception { - this.request.addParameter("name", ""); + request.addParameter("name", ""); MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertEquals("", result); @@ -459,7 +450,7 @@ public class RequestParamMethodArgumentResolverTests { Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); - this.request.addParameter("name", "123"); + request.addParameter("name", "123"); result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertEquals(123, ((Optional) result).get()); @@ -492,7 +483,7 @@ public class RequestParamMethodArgumentResolverTests { Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); - this.request.addParameter("name", "123", "456"); + request.addParameter("name", "123", "456"); result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.class, result.getClass()); assertArrayEquals(new Integer[] {123, 456}, (Integer[]) ((Optional) result).get()); @@ -525,7 +516,7 @@ public class RequestParamMethodArgumentResolverTests { Object result = resolver.resolveArgument(param, null, webRequest, binderFactory); assertEquals(Optional.empty(), result); - this.request.addParameter("name", "123", "456"); + request.addParameter("name", "123", "456"); result = resolver.resolveArgument(param, 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 67161464b18..40fa225f523 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 @@ -81,12 +81,13 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes } if (ex.getCause() instanceof Exception) { - ex = (Exception) ex.getCause(); - return doResolveException(request, response, handler, ex); + return doResolveException(request, response, handler, (Exception) ex.getCause()); } } catch (Exception resolveEx) { - logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx); + if (logger.isWarnEnabled()) { + logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx); + } } 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 79520443aa0..4efc3febea4 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 @@ -43,7 +43,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 *