Return MultiValueMap from ServerRequest.queryParams instead of List

This commit changes ServerRequest.queryParams from returning a
List<String> given a String name, to returning a
MultiValueMap<String, String>, which gives more flexibility.
This commit is contained in:
Arjen Poutsma 2017-07-03 16:23:33 +02:00
parent 2ccbc55ffd
commit f8589d9eca
7 changed files with 26 additions and 30 deletions

View File

@ -44,6 +44,7 @@ import org.springframework.http.server.reactive.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
@ -168,8 +169,8 @@ public class MockServerRequest implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
return Collections.unmodifiableList(this.queryParams.get(name));
public MultiValueMap<String, String> queryParams() {
return CollectionUtils.unmodifiableMultiValueMap(this.queryParams);
}
@Override

View File

@ -156,9 +156,8 @@ class DefaultServerRequest implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
List<String> queryParams = request().getQueryParams().get(name);
return queryParams != null ? queryParams : Collections.emptyList();
public MultiValueMap<String, String> queryParams() {
return request().getQueryParams();
}
@Override

View File

@ -535,8 +535,8 @@ public abstract class RequestPredicates {
}
@Override
public List<String> queryParams(String name) {
return this.request.queryParams(name);
public MultiValueMap<String, String> queryParams() {
return this.request.queryParams();
}
@Override

View File

@ -146,7 +146,7 @@ public interface ServerRequest {
* @return the parameter value
*/
default Optional<String> queryParam(String name) {
List<String> queryParams = this.queryParams(name);
List<String> queryParams = queryParams().get(name);
if (queryParams.isEmpty()) {
return Optional.empty();
}
@ -160,12 +160,9 @@ public interface ServerRequest {
}
/**
* Return all query parameter with the given name.
* <p>Returns an empty list if no values could be found.
* @param name the parameter name
* @return the parameter values
* Return all query parameters for this request.
*/
List<String> queryParams(String name);
MultiValueMap<String, String> queryParams();
/**
* Return the path variable with the given name, if present.
@ -184,13 +181,12 @@ public interface ServerRequest {
}
/**
* Return all path variables for the current request.
* @return a {@code Map} from path variable name to associated value
* Return all path variables for this request.
*/
Map<String, String> pathVariables();
/**
* Return the web session for the current request. Always guaranteed to
* Return the web session for this request. Always guaranteed to
* return an instance either matching to the session id requested by the
* client, or with a new session id either because the client did not
* specify one or because the underlying session had expired. Use of this

View File

@ -139,8 +139,8 @@ public class ServerRequestWrapper implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
return this.delegate.queryParams(name);
public MultiValueMap<String, String> queryParams() {
return this.delegate.queryParams();
}
@Override

View File

@ -44,6 +44,7 @@ import org.springframework.http.server.reactive.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractor;
@ -167,8 +168,8 @@ public class MockServerRequest implements ServerRequest {
}
@Override
public List<String> queryParams(String name) {
return Collections.unmodifiableList(this.queryParams.get(name));
public MultiValueMap<String, String> queryParams() {
return CollectionUtils.unmodifiableMultiValueMap(this.queryParams);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@ -18,7 +18,6 @@ package org.springframework.web.reactive.function.server.support;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@ -26,12 +25,12 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.server.ServerRequest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* @author Arjen Poutsma
@ -105,11 +104,11 @@ public class ServerRequestWrapperTests {
@Test
public void queryParams() throws Exception {
String name = "foo";
List<String> value = Collections.singletonList("bar");
when(mockRequest.queryParams(name)).thenReturn(value);
MultiValueMap<String, String> value = new LinkedMultiValueMap<>();
value.add("foo", "bar");
when(mockRequest.queryParams()).thenReturn(value);
assertSame(value, wrapper.queryParams(name));
assertSame(value, wrapper.queryParams());
}
@Test