Polishing
This commit is contained in:
@@ -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<K, V> extends Map<K, List<V>> {
|
||||
/**
|
||||
* 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<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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <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}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).
|
||||
* <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-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 {
|
||||
|
||||
|
||||
@@ -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;
|
||||
*
|
||||
* <p>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<String, String[]> parameterMap = webRequest.getParameterMap();
|
||||
if (MultiValueMap.class.isAssignableFrom(paramType)) {
|
||||
if (MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) {
|
||||
MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(parameterMap.size());
|
||||
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
|
||||
for (String value : entry.getValue()) {
|
||||
@@ -84,4 +78,5 @@ public class RequestParamMapMethodArgumentResolver implements HandlerMethodArgum
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
* <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
|
||||
|
||||
@@ -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<String, String> expected = new LinkedMultiValueMap<String, String>(1);
|
||||
MultiValueMap<String, String> 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) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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-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<String, String> expected = new LinkedMultiValueMap<String, String>(1);
|
||||
MultiValueMap<String, String> 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) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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:
|
||||
* <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>
|
||||
@@ -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:
|
||||
* <ul>
|
||||
* <li>annotated with {@code @RequestPart}
|
||||
* <li>of type {@link MultipartFile} unless annotated with {@code @RequestParam}
|
||||
* <li>of type {@code javax.servlet.http.Part} unless annotated with {@code @RequestParam}
|
||||
* <li>of type {@code javax.servlet.http.Part} unless annotated with
|
||||
* {@code @RequestParam}
|
||||
* </ul>
|
||||
*/
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user