【Translation】Java Constructs Recommended Postures for URL Request Links

In Java, we can use several libraries to dynamically add queries to URLs and ensure URL validity.

In this article, we'll learn how to use three of these libraries. All three libraries perform the exact same task, so we'll find that the generated URLs are the same.

2、Java EE 7 UriBuilder

The closest thing to a built-in Java solution is UriBuilder from javax.ws.rs-api , which we 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 in order to run our application.

The UriBuilder object provides a fromUri() method to create a base URI and a queryParam() method to add a query. Then, we can call the build() method which returns 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 and the query is added correctly.

3、Apache UriBuilder

Apache provides its own solution, UriBuilder in the HttpClient package . In order to use it, we need to add this 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 addParameter() 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 might make sense to use the UriComponentsBuilder provided by Spring. In order to use it, we need to add spring-web dependency in 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 via fromUriString() , then append the query using 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 other libraries, the build() method returns a UriComponents object, so to do it with a URI, we also need to call toURI().

5 Conclusion

In this article, we saw three ways to manipulate URLs in Java. We can add queries using the Java extension pack, Apache's UriBuilder, or the spring-web solution, each of which ensures valid URL structures and allows us to build them dynamically.

So deciding which method to use in our application depends on the packages and imports we have available, and the methods we already use. Each library provides a set of useful functions, so we should also consider whether other needs in the project can be satisfied at the same time.

As always, the full code for the examples can be found on GitHub .

Original address: https://www.baeldung.com/java-url-query-manipulation


Creation is not easy. If this article is helpful to you, please like, bookmark and pay attention. Your support and encouragement are the biggest motivation for my creation.
insert image description here

Welcome to join my Knowledge Planet, Knowledge Planet ID: 15165241 to communicate and learn together.
https://t.zsxq.com/Z3bAiea is marked from CSDN when applying.

Welcome to our slack workspace where you can ask questions of ai and me.
https://join.slack.com/t/ai-yx51081/shared_invite/zt-1t8cp1lk3-ZMAFutZcN3PCW~8WQDGjPg

Guess you like

Origin blog.csdn.net/w605283073/article/details/130445303