gpt4 book ai didi

java - UriComponentsBuilder 使用自定义方案截断

转载 作者:太空宇宙 更新时间:2023-11-04 09:25:23 25 4
gpt4 key购买 nike

我正在使用UriComponentsBuilder:

UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("com.stuff.mycompany:auth/stream/service");
allRequestParams.forEach((k, v) -> builder.queryParam(k, v)); // add all query params
URI uri = builder.encode().build().toUri();

当我运行此代码时,它返回com.stuff.mycompany:?query=whatever,并截断auth/stream/service。我注意到,如果我在冒号后添加两个斜杠,它就会按预期工作(com.stuff.mycompany:auth/stream/service)。

但是,我有一个要求,不能包含双斜杠。我怎样才能让它按预期工作?

最佳答案

你不能这么做。当您使用 UriComponentsBuilder.fromUriString 解析该字符串时,构建器内部结构如下所示:

scheme = "com.stuff.mycompany"
ssp = "auth/stream/service"

这里SSP是特定于方案的部分。看一下queryParam:

public UriComponentsBuilder queryParam(String name, Object... values) {
Assert.notNull(name, "Name must not be null");
if (!ObjectUtils.isEmpty(values)) {
for (Object value : values) {
String valueAsString = (value != null ? value.toString() : null);
this.queryParams.add(name, valueAsString);
}
}
else {
this.queryParams.add(name, null);
}
resetSchemeSpecificPart(); //Here is where you lose your SSP
return this;
}

看起来像 URI is not 的 Spring/Java 实现100% RFC 兼容,并且不支持对不透明 URL 的查询。考虑使用other implementations .

关于java - UriComponentsBuilder 使用自定义方案截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57758901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com