Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Uri.Builder and not just string and concatente parameters to it?

Tags:

java

string

uri

I was just wondering why it is advised to use a Uri.Builder instead of just using just strings and concatenating the parameters to it?.

Can someone please explain it with some short example?

Is it because of efficiency (because Strings are immutable..etc (but then we can use Stringbuilder or StringBuffer) )

Or there are any security issues involved with Strings?

like image 831
user3847870 Avatar asked Sep 26 '15 12:09

user3847870


1 Answers

An URI is quite complicated when you look at it from a distant perspective: there's a scheme, an authority, a path, queries... Moreover, you have to remember how to properly encode your data. All this is quite crumbersome and it is a repetitive task.

UriBuilder is designed to abstract you away from all this. It will properly encode your parameters and will properly build your URI without too much hassle and with a simple syntax:

UriBuilder.fromUri("http://localhost/").path("{a}").queryParam("name", "{value}").build("segment", "value");

will build the following URI: http://localhost/segment?name=value. {a} path will be replaced by the first parameter of build and {value} will be replaced by the second parameter.

like image 104
Tunaki Avatar answered Sep 23 '22 09:09

Tunaki