开发者

Standard Java Class for common URL/URI manipulation

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 pa开发者_C百科rameters
  • 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!


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.


I personnaly like UriBuilder from jax-rs


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜