Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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 jakarta.ws.rs-api, but if we import the jersey-commons the jakarta.ws.rs-api will come as a transitive dependency, thus we will not need to import specifically the jakarta.ws.rs.api just the jersey-common into our pom.xml:

<dependency>
     <groupId>org.glassfish.jersey.core</groupId>
     <artifactId>jersey-common</artifactId>
     <version>3.1.7</version>
     <scope>test</scope>
</dependency>

The latest version of jakarta.ws.rs-api can be found in the Maven Repository, also here can be found the latest version of the jersey-commons library.

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW