Dhruv Bhatnagar
Dhruv Bhatnagar

Reputation: 141

How to set multiple paths in URI builder?

How to make a URL for the below type? I'm doing something like below. If I send both the "path" and "PathSegments" values, it takes only PathSegment's values and not path's.

"https://localhost/api/customer" + "/" +{Id1} + "/" + "summary?invoiceId=" + {Id2} + "&page=0&pageSize=100&q=" + {Id3};

   public static String abcd(String path, List<NameValuePair> paramsLst, String...pathSegment){

    URIBuilder builder1 = new URIBuilder()

        .setScheme("https")
        .setHost("localhost")
        .setPath(path) // "/api/customer"
        .addParameter(paramsLst)
        .setPathSegments(pathSegment);

    return builder1.toString();
    }

Upvotes: 0

Views: 7668

Answers (1)

Georgii Lvov
Georgii Lvov

Reputation: 2743

You can try so:

URIBuilder uriBuilder = new URIBuilder();

uriBuilder.setScheme("https");
uriBuilder.setHost("localhost");
uriBuilder.setPathSegments("api", "customer", "id1Value", "summary");
uriBuilder.addParameters(paramsList);

Or you can try to use org.springframework.web.util.UriComponentsBuilder:

URI uri = UriComponentsBuilder.newInstance()
        .scheme("https")
        .host("localhost")
        .path("api/customer/{id1}/summary")
        .queryParams(params) // MultiValueMap
        .buildAndExpand("id1Value") // value for {id1} in the path
        .toUri();

Upvotes: 1

Related Questions