Is there a widely-used Java library that does something like what dojo.objectToQuery() does? E.g. (assuming the use of HttpCore's HttpParams object, but any key-value mapping will do):
HttpParams params = new BasicHttpParams()
.setParameter("foo", "bar")
.setParameter("thud", "grunt");
UnknownLibrary.toQueryString(params);
should yield "foo=bar&thud=grunt".
I know it's not hard to write but it seems like it should have already been written. I just can't find it.
Why reinvent the wheel? Apache HttpClient has URLEncodedUtils:
You don't really need a library for that. I didn't find one in HttpCore either so I wrote something like this,
I ended up writing my own. It can be called like
which isn't so bad.
If you have JAX-RS there is javax.ws.rs.core.UriBuilder; other than that you should primarily be aware of URLEncoder and URLDecoder which will at least encode the pieces of the parameter list.
There are plenty of libraries that can help you with URI building (don't reinvent the wheel). Here is one to get you started:
org.springframework:spring-web:4.2.5.RELEASE
See also: GIST > URI Builder Tests