Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of UriComponentsBuilder?

I get an example UriComponentsBuilder while learning spring boot. I don't know the meaning of UriComponentsBuilder and the function of UriComponentsBuilder.

like image 815
Cesar Wahyu Avatar asked Nov 17 '17 07:11

Cesar Wahyu


People also ask

What is UriComponentsBuilder?

public class UriComponentsBuilder extends Object implements UriBuilder, Cloneable. Builder for UriComponents . Typical usage involves: Create a UriComponentsBuilder with one of the static factory methods (such as fromPath(String) or fromUri(URI) )

Why we use UriComponentsBuilder?

The main advantages of UriComponentsBuilder are the flexibility of using URI template variables, and a possibility of injecting it directly into Spring Controller methods. All examples and configurations are available here on GitHub.

What is ServletUriComponentsBuilder spring boot?

public class ServletUriComponentsBuilder extends UriComponentsBuilder. UriComponentsBuilder with additional static factory methods to create links based on the current HttpServletRequest.

What is UriBuilder in Java?

public abstract class UriBuilder extends Object. URI template-aware utility class for building URIs from their components.


2 Answers

Yes, this used for constructing URI. Particularly useful when you want to invoke webservices in your class. Eg:

String baseUri = "/sample-uri"
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uri);
builder.queryParam("id", "1");
String uri= builder.build().encode().toUriString();

It is evident that you can pass additional params, to this. This provide a clean and efficient way of creating uris than writing those as plain text.

like image 199
Eldhose Abraham Avatar answered Oct 08 '22 16:10

Eldhose Abraham


It is a factory class for getting instances of UriComponents which are helpful for constructing URIs.

Please go through the tutorial below, examples are also given here.

http://www.baeldung.com/spring-uricomponentsbuilder

like image 20
Anirban Avatar answered Oct 08 '22 16:10

Anirban