gpt4 book ai didi

java - Resteasy UriBuilder 编码不正确?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:58 25 4
gpt4 key购买 nike

我正在尝试使用来自 RestEasy 的 UriBuilder 从字符串 url 创建一个 URI,但我得到了一些意想不到的结果。我正在运行以下代码。

UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8190/items?pageNumber={pageNumber}&pageSize={pageSize}");

System.out.println(uriBuilder.build(1, 10));

预期结果:

http://localhost:8190/items?pageNumber=1&pageSize=10

实际结果:

http://localhost:8190/items%3FpageNumber=1&pageSize=10

当使用 UriBuilder.fromUri() 而不是 fromPath() 时,它会在创建 URI 时抛出异常

查询中的非法字符位于索引 39:http://localhost:8190/items?pageNumber={pageNumber}&pageSize={pageSize}

39 处的字符是 {.

我不想解析完整的字符串来逐个创建 URI。

我查看了 RestEasy 代码,它正在编码 '?'使用 org.jboss.resteasy.util.Encode#pathEncoding 中的 pathEncoding 映射使用 org.jboss.resteasy.util.Encode#encode 创建构建器时的字符。

是我的用法不正确还是实现不正确?

最佳答案

RestEasy是一个 JAX-RS 实现,来自 Oracle documentation fromPath 的:

Create a new instance representing a relative URI initialized from a URI path.

我认为它不适用于绝对 URL,因此恐怕答案是您的用法不正确。

您将需要像 this 这样的东西(虽然没有测试)

UriBuilder.fromUri("http://localhost:8190/").
path("{a}").
queryParam("pageNumber", "{pageNumber}").
queryParam("pageSize", "{pageSize}").
build("items", 1,10);

关于java - Resteasy UriBuilder 编码不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13708435/

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