1. Overview

In Java, there are several libraries we can use to dynamically add queries to URLs while maintaining the URL’s validity.

In this article, we’ll learn how to use three of them. Each of the three performs exactly the same task. Therefore, we will see that the resulting URLs are the same.

2. Java EE 7 UriBuilder

The closest to an inbuilt Java solution is the UriBuilder found in javax.ws.rs-api, which we’ll need to import into our pom.xml:

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1.1</version>
</dependency>

The latest version can be found in the Maven Repository. We may also need to import jersey-commons for our application to run.

The UriBuilder object gives us the fromUri() method to create the base URI and queryParam() to add our query. We can then call build() to return a URI:

@Test
void whenUsingJavaUriBuilder_thenParametersAreCorrectlyAdded() {
    String url = "baeldung.com";
    String key = "article";
    String value = "beta";
    URI uri = UriBuilder.fromUri(url)
      .queryParam(key, value)
      .build();

    assertEquals("baeldung.com?article=beta", uri.toString());
}

As shown above, the URL looks as expected with the appended query.

3. Apache UriBuilder

Apache offers its own solution, the UriBuilder, in the HttpClient package. To use it, we’ll need to add it to our pom.xml:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

The latest version can be found in the Maven Repository.

To use it, we first call the URIBuilder constructor with our base URL String. Then use its builder method addParamter() to append our parameters and finally call build():

@Test
void whenUsingApacheUriBuilder_thenParametersAreCorrectlyAdded() {
    String url = "baeldung.com";
    String key = "article";
    String value = "alpha";
    URI uri = new URIBuilder(url).addParameter(key, value)
      .build();

    assertEquals("baeldung.com?article=alpha", uri.toString());
}

4. Spring UriComponentsBuilder

If we have a Spring application, it may make sense to use the Spring-provided UriComponentsBuilder. To use it, we need the spring-web dependency in our pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>6.0.6</version>
</dependency>

We can find the latest version in the Maven Repository.

We can use UriComponentsBuilder to create a URI with fromUriString() and then append our query with queryParam():

@Test
void whenUsingSpringUriComponentsBuilder_thenParametersAreCorrectlyAdded() {
    String url = "baeldung.com";
    String key = "article";
    String value = "charlie";
    URI uri = UriComponentsBuilder.fromUriString(url)
      .queryParam(key, value)
      .build()
      .toUri();

    assertEquals("baeldung.com?article=charlie", uri.toString());
}

Unlike the others, the build() method returns a UriComponents object, so to finish with a URI, we also need to call toURI().

5. Conclusion

In this article, we’ve seen three ways to manipulate our URLs in Java. We can add queries using Javas extended packages, Apaches UriBuilder, or the spring-web solution. Each gives us confidence that the URL structure will be valid while allowing us to build them dynamically.

For that reason deciding which to use for our application comes down to which packages and imports we have available and which we already use. Each of the libraries brings in a range of useful capabilities, so we should also consider if we can meet other needs within our project simultaneously.

As always, the full code for the examples is available over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.