Skip to content

Commit ffa032e

Browse files
committed
Polishing
1 parent f0f1979 commit ffa032e

File tree

9 files changed

+49
-60
lines changed

9 files changed

+49
-60
lines changed

spring-core/src/main/java/org/springframework/util/MultiValueMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public interface MultiValueMap<K, V> extends Map<K, List<V>> {
7575
void setAll(Map<K, V> values);
7676

7777
/**
78-
* Returns the first values contained in this {@code MultiValueMap}.
78+
* Return a {@code Map} with the first values contained in this {@code MultiValueMap}.
7979
* @return a single value representation of this map
8080
*/
8181
Map<K, V> toSingleValueMap();

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestPart.java

+17-16
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,24 @@
3333
* Annotation that can be used to associate the part of a "multipart/form-data" request
3434
* with a method argument.
3535
*
36-
* <p>Supported method argument types include {@link MultipartFile}
37-
* in conjunction with Spring's {@link MultipartResolver} abstraction,
38-
* {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests,
39-
* or otherwise for any other method argument, the content of the part is passed through an
40-
* {@link HttpMessageConverter} taking into consideration the 'Content-Type' header
41-
* of the request part. This is analogous to what @{@link RequestBody} does to resolve
42-
* an argument based on the content of a non-multipart regular request.
36+
* <p>Supported method argument types include {@link MultipartFile} in conjunction with
37+
* Spring's {@link MultipartResolver} abstraction, {@code javax.servlet.http.Part} in
38+
* conjunction with Servlet 3.0 multipart requests, or otherwise for any other method
39+
* argument, the content of the part is passed through an {@link HttpMessageConverter}
40+
* taking into consideration the 'Content-Type' header of the request part. This is
41+
* analogous to what @{@link RequestBody} does to resolve an argument based on the
42+
* content of a non-multipart regular request.
4343
*
44-
* <p>Note that @{@link RequestParam} annotation can also be used to associate the
45-
* part of a "multipart/form-data" request with a method argument supporting the same
46-
* method argument types. The main difference is that when the method argument is not a
47-
* String, @{@link RequestParam} relies on type conversion via a registered
48-
* {@link Converter} or {@link PropertyEditor} while @{@link RequestPart} relies
49-
* on {@link HttpMessageConverter HttpMessageConverters} taking into consideration the 'Content-Type' header
50-
* of the request part. @{@link RequestParam} is likely to be used with name-value form
51-
* fields while @{@link RequestPart} is likely to be used with parts containing more
52-
* complex content (e.g. JSON, XML).
44+
* <p>Note that @{@link RequestParam} annotation can also be used to associate the part
45+
* of a "multipart/form-data" request with a method argument supporting the same method
46+
* argument types. The main difference is that when the method argument is not a String
47+
* or raw {@code MultipartFile} / {@code Part}, {@code @RequestParam} relies on type
48+
* conversion via a registered {@link Converter} or {@link PropertyEditor} while
49+
* {@link RequestPart} relies on {@link HttpMessageConverter HttpMessageConverters}
50+
* taking into consideration the 'Content-Type' header of the request part.
51+
* {@link RequestParam} is likely to be used with name-value form fields while
52+
* {@link RequestPart} is likely to be used with parts containing more complex content
53+
* e.g. JSON, XML).
5354
*
5455
* @author Rossen Stoyanchev
5556
* @author Arjen Poutsma

spring-web/src/main/java/org/springframework/web/method/annotation/MapMethodProcessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,7 +60,7 @@ public boolean supportsReturnType(MethodParameter returnType) {
6060
}
6161

6262
@Override
63-
@SuppressWarnings({ "unchecked", "rawtypes" })
63+
@SuppressWarnings({"unchecked", "rawtypes"})
6464
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,
6565
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
6666

spring-web/src/main/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolver.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.springframework.web.method.support.UriComponentsContributor;
4242
import org.springframework.web.multipart.MultipartException;
4343
import org.springframework.web.multipart.MultipartFile;
44-
import org.springframework.web.multipart.MultipartHttpServletRequest;
44+
import org.springframework.web.multipart.MultipartRequest;
4545
import org.springframework.web.multipart.MultipartResolver;
4646
import org.springframework.web.multipart.support.MissingServletRequestPartException;
4747
import org.springframework.web.multipart.support.MultipartResolutionDelegate;
@@ -114,15 +114,11 @@ public RequestParamMethodArgumentResolver(@Nullable ConfigurableBeanFactory bean
114114
* Supports the following:
115115
* <ul>
116116
* <li>@RequestParam-annotated method arguments.
117-
* This excludes {@link Map} params where the annotation doesn't
118-
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
119-
* instead for such params.
120-
* <li>Arguments of type {@link MultipartFile}
121-
* unless annotated with @{@link RequestPart}.
122-
* <li>Arguments of type {@code javax.servlet.http.Part}
123-
* unless annotated with @{@link RequestPart}.
124-
* <li>In default resolution mode, simple type arguments
125-
* even if not with @{@link RequestParam}.
117+
* This excludes {@link Map} params where the annotation does not specify a name.
118+
* See {@link RequestParamMapMethodArgumentResolver} instead for such params.
119+
* <li>Arguments of type {@link MultipartFile} unless annotated with @{@link RequestPart}.
120+
* <li>Arguments of type {@code Part} unless annotated with @{@link RequestPart}.
121+
* <li>In default resolution mode, simple type arguments even if not with @{@link RequestParam}.
126122
* </ul>
127123
*/
128124
@Override
@@ -172,7 +168,7 @@ protected Object resolveName(String name, MethodParameter parameter, NativeWebRe
172168
}
173169

174170
Object arg = null;
175-
MultipartHttpServletRequest multipartRequest = request.getNativeRequest(MultipartHttpServletRequest.class);
171+
MultipartRequest multipartRequest = request.getNativeRequest(MultipartRequest.class);
176172
if (multipartRequest != null) {
177173
List<MultipartFile> files = multipartRequest.getFiles(name);
178174
if (!files.isEmpty()) {

spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMapMethodArgumentResolverTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class RequestHeaderMapMethodArgumentResolverTests {
6060

6161

6262
@Before
63-
public void setUp() throws Exception {
63+
public void setup() throws Exception {
6464
resolver = new RequestHeaderMapMethodArgumentResolver();
6565

6666
Method method = getClass().getMethod("params", Map.class, MultiValueMap.class, HttpHeaders.class, Map.class);

spring-web/src/test/java/org/springframework/web/method/annotation/RequestHeaderMethodArgumentResolverTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import static org.junit.Assert.*;
4545

4646
/**
47-
* Test fixture with {@link org.springframework.web.method.annotation.RequestHeaderMethodArgumentResolver}.
47+
* Test fixture with {@link RequestHeaderMethodArgumentResolver}.
4848
*
4949
* @author Arjen Poutsma
5050
* @author Rossen Stoyanchev
@@ -70,7 +70,7 @@ public class RequestHeaderMethodArgumentResolverTests {
7070

7171
@Before
7272
@SuppressWarnings("resource")
73-
public void setUp() throws Exception {
73+
public void setup() throws Exception {
7474
GenericWebApplicationContext context = new GenericWebApplicationContext();
7575
context.refresh();
7676
resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory());
@@ -94,7 +94,7 @@ public void setUp() throws Exception {
9494
}
9595

9696
@After
97-
public void teardown() {
97+
public void reset() {
9898
RequestContextHolder.resetRequestAttributes();
9999
}
100100

spring-web/src/test/java/org/springframework/web/method/annotation/RequestParamMethodArgumentResolverTests.java

+13-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.util.Optional;
2323
import javax.servlet.http.Part;
2424

25-
import org.junit.Before;
2625
import org.junit.Test;
2726

2827
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
@@ -53,31 +52,23 @@
5352
import static org.springframework.web.method.MvcAnnotationPredicates.*;
5453

5554
/**
56-
* Test fixture with {@link org.springframework.web.method.annotation.RequestParamMethodArgumentResolver}.
55+
* Test fixture with {@link RequestParamMethodArgumentResolver}.
5756
*
5857
* @author Arjen Poutsma
5958
* @author Rossen Stoyanchev
6059
* @author Brian Clozel
6160
*/
6261
public class RequestParamMethodArgumentResolverTests {
6362

64-
private RequestParamMethodArgumentResolver resolver;
63+
private RequestParamMethodArgumentResolver resolver = new RequestParamMethodArgumentResolver(null, true);
6564

66-
private NativeWebRequest webRequest;
65+
private MockHttpServletRequest request = new MockHttpServletRequest();
6766

68-
private MockHttpServletRequest request;
67+
private NativeWebRequest webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
6968

7069
private ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();
7170

7271

73-
@Before
74-
public void setup() throws Exception {
75-
resolver = new RequestParamMethodArgumentResolver(null, true);
76-
request = new MockHttpServletRequest();
77-
webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
78-
}
79-
80-
8172
@Test
8273
public void supportsParameter() {
8374
resolver = new RequestParamMethodArgumentResolver(null, true);
@@ -385,7 +376,7 @@ public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
385376
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
386377
given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
387378

388-
this.request.addParameter("stringNotAnnot", "");
379+
request.addParameter("stringNotAnnot", "");
389380

390381
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
391382
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
@@ -400,7 +391,7 @@ public void missingRequestParamEmptyValueNotRequired() throws Exception {
400391
WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
401392
given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
402393

403-
this.request.addParameter("name", "");
394+
request.addParameter("name", "");
404395

405396
MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
406397
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
@@ -426,23 +417,23 @@ public void resolveSimpleTypeParamToNull() throws Exception {
426417

427418
@Test // SPR-10180
428419
public void resolveEmptyValueToDefault() throws Exception {
429-
this.request.addParameter("name", "");
420+
request.addParameter("name", "");
430421
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
431422
Object result = resolver.resolveArgument(param, null, webRequest, null);
432423
assertEquals("bar", result);
433424
}
434425

435426
@Test
436427
public void resolveEmptyValueWithoutDefault() throws Exception {
437-
this.request.addParameter("stringNotAnnot", "");
428+
request.addParameter("stringNotAnnot", "");
438429
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
439430
Object result = resolver.resolveArgument(param, null, webRequest, null);
440431
assertEquals("", result);
441432
}
442433

443434
@Test
444435
public void resolveEmptyValueRequiredWithoutDefault() throws Exception {
445-
this.request.addParameter("name", "");
436+
request.addParameter("name", "");
446437
MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
447438
Object result = resolver.resolveArgument(param, null, webRequest, null);
448439
assertEquals("", result);
@@ -459,7 +450,7 @@ public void resolveOptionalParamValue() throws Exception {
459450
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
460451
assertEquals(Optional.empty(), result);
461452

462-
this.request.addParameter("name", "123");
453+
request.addParameter("name", "123");
463454
result = resolver.resolveArgument(param, null, webRequest, binderFactory);
464455
assertEquals(Optional.class, result.getClass());
465456
assertEquals(123, ((Optional) result).get());
@@ -492,7 +483,7 @@ public void resolveOptionalParamArray() throws Exception {
492483
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
493484
assertEquals(Optional.empty(), result);
494485

495-
this.request.addParameter("name", "123", "456");
486+
request.addParameter("name", "123", "456");
496487
result = resolver.resolveArgument(param, null, webRequest, binderFactory);
497488
assertEquals(Optional.class, result.getClass());
498489
assertArrayEquals(new Integer[] {123, 456}, (Integer[]) ((Optional) result).get());
@@ -525,7 +516,7 @@ public void resolveOptionalParamList() throws Exception {
525516
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
526517
assertEquals(Optional.empty(), result);
527518

528-
this.request.addParameter("name", "123", "456");
519+
request.addParameter("name", "123", "456");
529520
result = resolver.resolveArgument(param, null, webRequest, binderFactory);
530521
assertEquals(Optional.class, result.getClass());
531522
assertEquals(Arrays.asList("123", "456"), ((Optional) result).get());

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ protected ModelAndView doResolveException(
8181
}
8282

8383
if (ex.getCause() instanceof Exception) {
84-
ex = (Exception) ex.getCause();
85-
return doResolveException(request, response, handler, ex);
84+
return doResolveException(request, response, handler, (Exception) ex.getCause());
8685
}
8786
}
8887
catch (Exception resolveEx) {
89-
logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx);
88+
if (logger.isWarnEnabled()) {
89+
logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", resolveEx);
90+
}
9091
}
9192
return null;
9293
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
/**
4444
* Resolves the following method arguments:
4545
* <ul>
46-
* <li>Annotated with {@code @RequestPart}
46+
* <li>Annotated with @{@link RequestPart}
4747
* <li>Of type {@link MultipartFile} in conjunction with Spring's {@link MultipartResolver} abstraction
4848
* <li>Of type {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests
4949
* </ul>

0 commit comments

Comments
 (0)