Polishing
This commit is contained in:
parent
f0f1979fc5
commit
ffa032e78f
|
|
@ -75,7 +75,7 @@ public interface MultiValueMap<K, V> extends Map<K, List<V>> {
|
|||
void setAll(Map<K, V> 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<K, V> toSingleValueMap();
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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).
|
||||
* <p>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
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
* <ul>
|
||||
* <li>@RequestParam-annotated method arguments.
|
||||
* This excludes {@link Map} params where the annotation doesn't
|
||||
* specify a name. See {@link RequestParamMapMethodArgumentResolver}
|
||||
* instead for such params.
|
||||
* <li>Arguments of type {@link MultipartFile}
|
||||
* unless annotated with @{@link RequestPart}.
|
||||
* <li>Arguments of type {@code javax.servlet.http.Part}
|
||||
* unless annotated with @{@link RequestPart}.
|
||||
* <li>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.
|
||||
* <li>Arguments of type {@link MultipartFile} unless annotated with @{@link RequestPart}.
|
||||
* <li>Arguments of type {@code Part} unless annotated with @{@link RequestPart}.
|
||||
* <li>In default resolution mode, simple type arguments even if not with @{@link RequestParam}.
|
||||
* </ul>
|
||||
*/
|
||||
@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<MultipartFile> files = multipartRequest.getFiles(name);
|
||||
if (!files.isEmpty()) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ import org.springframework.web.multipart.support.RequestPartServletServerHttpReq
|
|||
/**
|
||||
* Resolves the following method arguments:
|
||||
* <ul>
|
||||
* <li>Annotated with {@code @RequestPart}
|
||||
* <li>Annotated with @{@link RequestPart}
|
||||
* <li>Of type {@link MultipartFile} in conjunction with Spring's {@link MultipartResolver} abstraction
|
||||
* <li>Of type {@code javax.servlet.http.Part} in conjunction with Servlet 3.0 multipart requests
|
||||
* </ul>
|
||||
|
|
|
|||
Loading…
Reference in New Issue