|
55 | 55 | */
|
56 | 56 | public class RequestParamArgumentResolver extends AbstractNamedValueArgumentResolver {
|
57 | 57 |
|
58 |
| - private boolean formatAsSingleValue = true; |
| 58 | + private boolean favorSingleValue; |
59 | 59 |
|
60 | 60 |
|
61 | 61 | public RequestParamArgumentResolver(ConversionService conversionService) {
|
62 | 62 | super(conversionService);
|
63 | 63 | }
|
64 | 64 |
|
65 |
| - public RequestParamArgumentResolver(ConversionService conversionService, boolean formatAsSingleValue) { |
66 |
| - super(conversionService); |
67 |
| - this.formatAsSingleValue = formatAsSingleValue; |
68 |
| - } |
69 |
| - |
70 | 65 |
|
71 |
| - @Override |
72 |
| - @Nullable |
73 |
| - protected NamedValueInfo createNamedValueInfo(MethodParameter parameter, HttpRequestValues.Metadata requestValues) { |
74 |
| - MediaType contentType = requestValues.getContentType(); |
75 |
| - if (contentType != null && isMultiValueFormContentType(contentType)) { |
76 |
| - this.formatAsSingleValue = true; |
77 |
| - } |
| 66 | + /** |
| 67 | + * Whether to format multiple values (e.g. collection, array) as a single |
| 68 | + * String value through the configured {@link ConversionService} unless the |
| 69 | + * content type is form data, or it is a multipart request. |
| 70 | + * <p>By default, this is {@code false} in which case formatting is not applied, |
| 71 | + * and a separate parameter with the same name is created for each value. |
| 72 | + * @since 6.2 |
| 73 | + */ |
| 74 | + public void setFavorSingleValue(boolean favorSingleValue) { |
| 75 | + this.favorSingleValue = favorSingleValue; |
| 76 | + } |
78 | 77 |
|
79 |
| - return createNamedValueInfo(parameter); |
| 78 | + /** |
| 79 | + * Return the setting for {@link #setFavorSingleValue favorSingleValue}. |
| 80 | + * @since 6.2 |
| 81 | + */ |
| 82 | + public boolean isFavorSingleValue() { |
| 83 | + return this.favorSingleValue; |
80 | 84 | }
|
81 | 85 |
|
| 86 | + |
82 | 87 | @Override
|
83 | 88 | @Nullable
|
84 |
| - protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { |
| 89 | + protected NamedValueInfo createNamedValueInfo(MethodParameter parameter, HttpRequestValues.Metadata metadata) { |
85 | 90 | RequestParam annot = parameter.getParameterAnnotation(RequestParam.class);
|
86 | 91 | if (annot == null) {
|
87 | 92 | return null;
|
88 | 93 | }
|
89 |
| - |
90 |
| - return (annot == null ? null : |
91 |
| - new NamedValueInfo(annot.name(), annot.required(), annot.defaultValue(), |
92 |
| - "request parameter", this.formatAsSingleValue)); |
| 94 | + return new NamedValueInfo( |
| 95 | + annot.name(), annot.required(), annot.defaultValue(), "request parameter", |
| 96 | + supportsMultipleValues(parameter, metadata)); |
93 | 97 | }
|
94 | 98 |
|
95 | 99 | @Override
|
96 |
| - protected void addRequestValue( |
97 |
| - String name, Object value, MethodParameter parameter, HttpRequestValues.Builder requestValues) { |
98 |
| - |
99 |
| - requestValues.addRequestParameter(name, (String) value); |
| 100 | + protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { |
| 101 | + // Shouldn't be called since we override createNamedValueInfo with HttpRequestValues.Metadata |
| 102 | + throw new UnsupportedOperationException(); |
100 | 103 | }
|
101 | 104 |
|
102 |
| - protected boolean isFormatAsSingleValue() { |
103 |
| - return this.formatAsSingleValue; |
| 105 | + /** |
| 106 | + * Determine whether the resolver should send multi-value request parameters |
| 107 | + * as individual values. If not, they are formatted to a single String value. |
| 108 | + * The default implementation uses {@link #isFavorSingleValue()} to decide |
| 109 | + * unless the content type is form data, or it is a multipart request. |
| 110 | + * @since 6.2 |
| 111 | + */ |
| 112 | + protected boolean supportsMultipleValues(MethodParameter parameter, HttpRequestValues.Metadata metadata) { |
| 113 | + return (!isFavorSingleValue() || isFormOrMultipartContent(metadata)); |
104 | 114 | }
|
105 | 115 |
|
106 |
| - protected void setFormatAsSingleValue(boolean formatAsSingleValue) { |
107 |
| - this.formatAsSingleValue = formatAsSingleValue; |
| 116 | + /** |
| 117 | + * Whether the content type is form data, or it is a multipart request. |
| 118 | + * @since 6.2 |
| 119 | + */ |
| 120 | + protected boolean isFormOrMultipartContent(HttpRequestValues.Metadata metadata) { |
| 121 | + MediaType mediaType = metadata.getContentType(); |
| 122 | + return (mediaType != null && (mediaType.getType().equals("multipart") || |
| 123 | + mediaType.isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED))); |
108 | 124 | }
|
109 | 125 |
|
110 |
| - protected boolean isMultiValueFormContentType(MediaType contentType) { |
111 |
| - return contentType.equals(MediaType.APPLICATION_FORM_URLENCODED) |
112 |
| - || contentType.getType().equals("multipart"); |
| 126 | + @Override |
| 127 | + protected void addRequestValue( |
| 128 | + String name, Object value, MethodParameter parameter, HttpRequestValues.Builder requestValues) { |
| 129 | + |
| 130 | + requestValues.addRequestParameter(name, (String) value); |
113 | 131 | }
|
114 | 132 |
|
115 | 133 | }
|
0 commit comments