Extending base URL with path segments

In this post under JAX-RS Client, I will show with example how to extend base URL with path segments.

Suppose you want to access below two REST apis. These two apis return user information with id 2 and 3.


To access these two urls you will create two “WebTarget” instances as shown below

    WebTarget webTarget1 = client.target("https://jsonplaceholder.typicode.com/users/2");
    WebTarget webTarget2 = client.target("https://jsonplaceholder.typicode.com/users/3");

If you observe correctly, the base url “https://jsonplaceholder.typicode.com” is common, only the path segments (i.e., “users/2”) after the base url varies.

In the above approach the base url is repeated twice.

Tomorrow any change in base url requires change in two places.

To avoid changing base url at two places, we can use alternative approach, which is declaring a variable which will have base url say “baseUrl” as shown below

    String baseUrl = "https://jsonplaceholder.typicode.com";

Then add the remaining path segments using “path” method of “WebTarget” instances as shown below

    String baseUrl = "https://jsonplaceholder.typicode.com";

    WebTarget webTarget1 = client.target(baseUrl);
    webTarget1 = webTarget1.path("users").path("2");

    WebTarget webTarget2 = client.target(baseUrl);
    webTarget2 = webTarget2.path("users").path("3");    

As a result of which, repetition of baseUrl is avoided.

In this way using the “path” method of “WebTarget” instance we can add more path segments to the base url.

Below is the complete code for your reference.

Main class

1  package defaultPackage;
3  import jakarta.ws.rs.client.Client;
4  import jakarta.ws.rs.client.ClientBuilder;
5  import jakarta.ws.rs.client.Invocation;
6  import jakarta.ws.rs.client.WebTarget;
7  import jakarta.ws.rs.core.Response;
9  public class Example5 {
10     public static void main(String[] args) {
11         String baseUrl = "https://jsonplaceholder.typicode.com";
12         Client client = ClientBuilder.newClient();
13         WebTarget webTarget1 = client.target(baseUrl);
14         webTarget1 = webTarget1.path("users").path("2");
15         Invocation.Builder builder = webTarget1.request();
16         Invocation invocation = builder.buildGet();
17         Response response = invocation.invoke();
18         if(response.getStatus() == 200) {
19             System.out.println("Successful");
20             String result = response.readEntity(String.class);
21             System.out.println(result);
22         } else {
23             System.out.println("Failed");
24         }
25         System.out.println("===============================================");
26         WebTarget webTarget2 = client.target(baseUrl);
27         webTarget2 = webTarget2.path("users").path("3");
28         builder = webTarget2.request();
29         invocation = builder.buildGet();
30         response = invocation.invoke();
31         if(response.getStatus() == 200) {
32             System.out.println("Successful");
33             String result = response.readEntity(String.class);
34             System.out.println(result);
35         } else {
36             System.out.println(invocation.toString());
37             System.out.println("Failed");
38         }
39         client.close();
40     }
41 }

In the above code, at line 11, we create a String variable “baseUrl” which will store the base url “https://jsonplaceholder.typicode.com”.

At line 13, we create a “WebTarget” instance by name “webTarget1” using the “baseUrl”.

At line 14, we append the “baseUrl” with new path segments by taking the help of “path” method on “webTarget1” instance.

At line 17, we make a request to the resource at url “https://jsonplaceholder.typicode.com/users/2”.

Similarly at line 26, we create another “WebTarget” instance by name “webTarget2” using the “baseUrl”.

At line 27, we append the “baseUrl” with new path segments by taking the help of “path” method on “webTarget2” instance.

At line 30, we make a request to the resource at url “https://jsonplaceholder.typicode.com/users/3”

Leave a Reply