从URL中获取请求参数Map
String url =“http://jd.com?a=1&b=2&c=2&a=2&a=${test}”;
-> map
1、使用UriComponentsBuilder
import org.junit.jupiter.api.Test;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
@Test
public void testGetRequestParams(){
String url ="http://jd.com?a=1&b=2&c=2&a=2&a=${test}";
MultiValueMap<String, String> queryParams = UriComponentsBuilder.fromHttpUrl(url).build().getQueryParams();
System.out.println(queryParams);
}
结果:
{a=[1, 2, ${test}], b=[2], c=[2]}
2、使用hutool工具
import cn.hutool.http.HttpUtil;
import java.nio.charset.StandardCharsets;
@Test
public void testHutool(){
String url ="http://jd.com?a=1&b=2&c=2&a=2&a=${test}";
Map<String, List<String>> stringListMap = HttpUtil.decodeParams(url, "UTF-8");
System.out.println("decodeParams:" + stringListMap);
// 获取单值map最后一个会覆盖上一个
Map<String,String> stringStringMap = HttpUtil.decodeParamMap(url, StandardCharsets.UTF_8);
System.out.println("decodeParamMap:" + stringStringMap);
// map 转 URL params
String urlParams = HttpUtil.toParams(stringStringMap);
System.out.println("urlParams: "+urlParams);
}
结果:
decodeParams:{a=[1, 2, ${test}], b=[2], c=[2]}
decodeParamMap:{a=${test}, b=2, c=2}
urlParams: a=$%7Btest%7D&b=2&c=2
3、Java 8实现
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
@Test
public void testURL() throws MalformedURLException {
String url ="http://jd.com?a=1&b=2&c=2&a=2&a=${test}";
System.out.println(splitQuery(new URL(url)));
}
public Map<String, List<String>> splitQuery(URL url){
if (Strings.isNullOrEmpty(url.getQuery())) {
return Collections.emptyMap();
}
return Arrays.stream(url.getQuery().split("&"))
.map(this::splitQueryParameter)
.collect(Collectors.groupingBy(SimpleImmutableEntry::getKey, LinkedHashMap::new, mapping(Map.Entry::getValue, toList())));
}
public SimpleImmutableEntry<String, String> splitQueryParameter(String it) {
final int idx = it.indexOf("=");
final String key = idx > 0 ? it.substring(0, idx) : it;
final String value = idx > 0 && it.length() > idx + 1 ? it.substring(idx + 1) : null;
try {
return new SimpleImmutableEntry<>(
URLDecoder.decode(key, StandardCharsets.UTF_8.name()),
URLDecoder.decode(value, StandardCharsets.UTF_8.name())
);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
结果:
{a=[1, 2, ${test}], b=[2], c=[2]}
4、使用org.apache.http.client.utils.URLEncodedUtils
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
@Test
public void test04() throws UnsupportedEncodingException, URISyntaxException {
String url ="http://jd.com?a=1&b=2&c=2&a=2&a=$%7Btest%7D";
List<NameValuePair> params = URLEncodedUtils.parse(new URI(url).getQuery(), StandardCharsets.UTF_8);
for (NameValuePair param : params) {
System.out.println(param.getName() + " : " + param.getValue());
}
System.out.println(params);
}
结果:
a : 1
b : 2
c : 2
a : 2
a : ${test}
[a=1, b=2, c=2, a=2, a=${test}]
5、java11 实现
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import java.util.stream.Collectors;
@Test
public void test05() throws MalformedURLException {
String url ="http://jd.com?a=1&b=2&c=2&a=2&a=$%7Btest%7D";
List<Map.Entry<String, String>> list = Pattern.compile("&")
.splitAsStream(new URL(url).getQuery())
.map(s -> Arrays.copyOf(s.split("=", 2), 2))
.map(o -> Map.entry(decode(o[0]), decode(o[1])))
.collect(Collectors.toList());
System.out.println(list);
Map<String, List<String>> map = Pattern.compile("&")
.splitAsStream(new URL(url).getQuery())
.map(s -> Arrays.copyOf(s.split("=", 2), 2))
.collect(groupingBy(s -> decode(s[0]), mapping(s -> decode(s[1]), toList())));
System.out.println(map);
}
private static String decode(final String encoded) {
return Optional.ofNullable(encoded)
.map(e -> URLDecoder.decode(e, StandardCharsets.UTF_8))
.orElse(null);
}
结果:
[a=1, b=2, c=2, a=2, a=${test}]
{a=[1, 2, ${test}], b=[2], c=[2]}
参考:
https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection