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/hote…


This content originally appeared on DEV Community and was authored by Salad Lam

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());


This content originally appeared on DEV Community and was authored by Salad Lam


Print Share Comment Cite Upload Translate Updates
APA

Salad Lam | Sciencx (2025-01-03T22:54:09+00:00) About UriComponentsBuilder and UriComponents. Retrieved from https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/

MLA
" » About UriComponentsBuilder and UriComponents." Salad Lam | Sciencx - Friday January 3, 2025, https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/
HARVARD
Salad Lam | Sciencx Friday January 3, 2025 » About UriComponentsBuilder and UriComponents., viewed ,<https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/>
VANCOUVER
Salad Lam | Sciencx - » About UriComponentsBuilder and UriComponents. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/
CHICAGO
" » About UriComponentsBuilder and UriComponents." Salad Lam | Sciencx - Accessed . https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/
IEEE
" » About UriComponentsBuilder and UriComponents." Salad Lam | Sciencx [Online]. Available: https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/. [Accessed: ]
rf:citation
» About UriComponentsBuilder and UriComponents | Salad Lam | Sciencx | https://www.scien.cx/2025/01/03/about-uricomponentsbuilder-and-uricomponents/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.