19

This question has almost certainly been asked before, but I ask it anyway because I couldn't find an answer.

Generally, is there a utility class of some sort that assists in common String manipulations associated with URL/URIs?

I'm thinking something like Java SE's URL Class, but maybe a little beefier. I'm looking for something that will let you do simple things, like:

  • Get a List of query string parameters
  • An "addParameter" method to add a query string parameter, and it will take care of adding "&", "?", and "=" where necessary
  • Also, encoding parameter values would be ideal...

Let me know, thanks!

javanna
  • 59,145
  • 14
  • 144
  • 125
JasonStoltz
  • 3,040
  • 4
  • 27
  • 37
  • 1
    Good question. I couldn't find a good one so rolled my own. I'd like to see the suggestions here. – Joel May 06 '11 at 15:33
  • 2
    It is a well-known and much-complained-about lacuna that there is nothing in Java SE that can do anything interesting with query strings. For everything else, there's java.net.URI, which i would suggest be preferred to java.net.URL, because, amongst other things, it has a sane equals() implementation. – Tom Anderson May 09 '11 at 20:23
  • check out Galimatias - a URL parsing and normalization library written in Java https://github.com/smola/galimatias – Dharmendar Kumar 'DK' Oct 31 '17 at 17:13

3 Answers3

8

There isn't really (oddly enough) any standard that does it all. There are some bits and pieces, usually buried in various util packages:

I've used http://java.net/projects/urlencodedquerystring/pages/Home to decent effect (for extraction of parameters).

Atlassian's JIRA has http://docs.atlassian.com/jira/4.2/index.html?com/atlassian/jira/util/UrlBuilder.html, which I've actually extracted from the jar and used.

On Android, http://developer.android.com/reference/android/net/Uri.Builder.html is a Uri builder that works pretty well as far as building a url with ease.

And finally, in a classic case of history repeating itself: A good library to do URL Query String manipulation in Java.

I'd really just rip out the android.net.Uri.Builder class and pair that with the urlencodedquerystring class and then carry those around with you, but this does seem like a good candidate for an Apache commons package.

Community
  • 1
  • 1
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Awesome, thanks for the great reply! I've run into similar code in some various proprietary libraries before as well, which is what sparked this question. I think I'll take a stab at your suggestion! I'll leave this question open for a little while longer to see if I get any additional replies. – JasonStoltz May 09 '11 at 18:34
  • Accepting this answer. Additionally, Tom Anderson's comment on the main question was helpful as well. – JasonStoltz May 10 '11 at 12:47
  • Here's another question that has a suggestion for creating a url encoded query string from a Map. http://stackoverflow.com/questions/2809877/how-to-convert-map-to-url-query-string/2810102#2810434 – JasonStoltz May 10 '11 at 13:17
  • 1
    This has also been discussed on the Guava issues page http://code.google.com/p/guava-libraries/issues/detail?id=1005 They havent currently implemented there own library but someone suggests using the UriBuilder provided by Jersey – reevesy Jul 29 '13 at 11:15
  • The Apache http components also come with a [URI builder](https://hc.apache.org/httpcomponents-client-4.4.x/httpclient/apidocs/org/apache/http/client/utils/URIBuilder.html). Unfortunately it is in the http client rather than in the http core module, which I would have expected. – Felix Leipold Jun 25 '15 at 10:24
  • I'd add that the OkHttp library comes with a [HttpUrl](https://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/HttpUrl.html) class which allows you to construct and manipulate URL's. Easy to use and works well, in my experience. – Jonas Czech Mar 29 '16 at 13:50
1

I personnaly like UriBuilder from jax-rs

Vinze
  • 2,549
  • 3
  • 22
  • 23
0

This does not answer OP's question directly (i.e. it's not a generic, all-around library for URL manipulation), but: if you're going to be using Spring anyway, you might as well consider the ServletUriComponentsBuilder and UriComponentsBuilder classes (see here and here for javadocs).

I believe they are bundled with the spring-web dependency. IMHO, these offer quite a few convenient utility methods for working with URIs, URLs and query parameters.

Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40