I've got the URI like this:
https://google.com.ua/oauth/authorize?client_id=SS&response_type=code&scope=N_FULL&access_type=offline&redirect_uri=http://localhost/Callback
I need a collection with parsed elements:
NAME VALUE
------------------------
client_id SS
response_type code
scope N_FULL
access_type offline
redirect_uri http://localhost/Callback
To be exact, I need a Java equivalent for C# HttpUtility.ParseQueryString Method. Please, give me an advice on this. Thanks.
The shortest way I've found is this one:
UPDATE:
UriComponentsBuilder
comes from Spring. Here the link.If you are using Spring, add an argument of type
@RequestParam Map<String,String>
to your controller method, and Spring will construct the map for you!If you are looking for a way to achieve it without using an external library, the following code will help you.
You can access the returned Map using
<map>.get("client_id")
, with the URL given in your question this would return "SS".UPDATE URL-Decoding added
UPDATE As this answer is still quite popular, I made an improved version of the method above, which handles multiple parameters with the same key and parameters with no value as well.
UPDATE Java8 version
Running the above method with the URL
https://stackoverflow.com?param1=value1¶m2=¶m3=value3¶m3
returns this Map:
If you are using Spring Framework:
You will get:
If you're using Java 8 and you're willing to write a few reusable methods, you can do it in one line.
But that's a pretty brutal line.
If you are using servlet doGet try this