我正在尝试使用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时抛出异常
Illegal character in query at index 39: http://localhost:8190/items?pageNumber={pageNumber}&pageSize={pageSize}
39处的字符是{。
我不想为了逐个部分地创建URI而解析完整的字符串。
我查看了RestEasy代码,它正在编码“?”字符,同时使用org.jboss.resteasy.util.Encode#encode使用org.jboss.resteasy.util.Encode#pathEncoding中的pathEncoding映射创建构建器。
是我的用法不正确还是实现不正确?
发布于 2012-12-05 01:32:36
由于RestEasy是一个JAX-RS实现,因此可以从fromPath的Oracle documentation
创建一个新实例,表示从URI路径初始化的相对 URI。
我认为它不是针对绝对URL的,因此我担心答案是您的用法不正确。
你需要像this这样的东西(虽然没有测试它)
UriBuilder.fromUri("http://localhost:8190/").
path("{a}").
queryParam("pageNumber", "{pageNumber}").
queryParam("pageSize", "{pageSize}").
build("items", 1,10);https://stackoverflow.com/questions/13708435
复制相似问题