DEV Community

Salad Lam
Salad Lam

Posted on

About UriComponentsBuilder and UriComponents

UriComponentsBuilder and UriComponents are the utility class in Spring Framework for building and modifying URLs. Following is the test to show how to use them.

UriComponentsBuilder b1 = UriComponentsBuilder.fromHttpUrl("https://www.example.com/hotels/42?filter=f1&filter=f2&option&query=#hash");
UriComponents c1 = b1.build();
assertEquals("https", c1.getScheme());
assertEquals("www.example.com", c1.getHost());
assertEquals(-1, c1.getPort());
assertEquals(Lists.list("hotels", "42"), c1.getPathSegments());
assertEquals("f1", c1.getQueryParams().getFirst("filter"));
assertNull(c1.getQueryParams().getFirst("option"));
assertEquals("", c1.getQueryParams().getFirst("query"));
assertEquals("hash", c1.getFragment());

UriComponentsBuilder b2 = b1.cloneBuilder();
b2.path("/info");
assertEquals("https://www.example.com/hotels/42/info?filter=f1&filter=f2&option&query=#hash", b2.build().toUriString());

UriComponentsBuilder b3 = b1.cloneBuilder();
b3.replacePath("/info/hotels/42");
assertEquals("https://www.example.com/info/hotels/42?filter=f1&filter=f2&option&query=#hash", b3.build().toUriString());

UriComponentsBuilder b4 = b1.cloneBuilder();
b4.replaceQuery(null);
b4.fragment(null);
b4.userInfo("user1");
assertEquals("https://user1@www.example.com/hotels/42", b4.build().toUriString());

UriComponentsBuilder b5 = b1.cloneBuilder();
b5.queryParam("query", "q1", "q2");
assertEquals("https://www.example.com/hotels/42?filter=f1&filter=f2&option&query=&query=q1&query=q2#hash", b5.build().toUriString());

UriComponentsBuilder b6 = b1.cloneBuilder();
b6.replaceQueryParam("query", "q1", "q2");
assertEquals("https://www.example.com/hotels/42?filter=f1&filter=f2&option&query=q1&query=q2#hash", b6.build().toUriString());

assertEquals("/hotels/42?filter=hot&cold", UriComponentsBuilder.fromUriString("/hotels/42").query("filter={value}").buildAndExpand(Collections.singletonMap("value", "hot&cold")).toUriString());
Enter fullscreen mode Exit fullscreen mode

Billboard image

Synthetic monitoring. Built for developers.

Join Vercel, Render, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay