Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resteasy UriBuilder incorrectly encodes?

I am trying to create a URI from a string url using UriBuilder from RestEasy and I am getting some unexpected results. I am running the following piece of code.

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

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

Expected result:

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

Actual result:

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

When using UriBuilder.fromUri() instead of fromPath() it throws an exception while creating the URI

Illegal character in query at index 39: http://localhost:8190/items?pageNumber={pageNumber}&pageSize={pageSize}

the character at 39 is {.

I don't want to parse the complete string to create the URI part by part.

I looked at the RestEasy code and it is encoding the '?' character while creating the builder using org.jboss.resteasy.util.Encode#encode using the pathEncoding map from org.jboss.resteasy.util.Encode#pathEncoding.

Is my usage incorrect or the implementation incorrect?

like image 549
Saket Avatar asked Dec 06 '25 06:12

Saket


1 Answers

Since RestEasy is a JAX-RS implementation, from the Oracle documentation of fromPath:

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

I think it was not intended for absolute URLs, hence I'm afraid the answer is that your usage is incorrect.

You will need something like this (didn't test it though)

UriBuilder.fromUri("http://localhost:8190/").
  path("{a}").
  queryParam("pageNumber", "{pageNumber}").
  queryParam("pageSize", "{pageSize}").
  build("items", 1,10);
like image 70
Eran Medan Avatar answered Dec 08 '25 02:12

Eran Medan