UriComponentsBuilder使用自定义方案截断

时间:2019-09-02 14:44:43

标签: java spring

我这样使用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)。

但是,我必须遵循并且不能包含双斜杠。如何使它按预期工作?

1 个答案:

答案 0 :(得分:0)

您不能那样做。当您使用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