In my setup, I get all the paths for my resources from the REST API from an initial call to the API. We use this pattern to be able to change all the resource paths without breaking all existing app versions in the process.
I have been playing around with Retrofit and I tried to create a method that would accept any path I pass to it as a string. My try looks like this
@GET("/{path}")
public FooBar getFooBar(@Path("path") String path);
String path = "foo/bar";
api.getFooBar(path);
/foo%2Fbar
/foo/bar
Use @EncodedPath
! That's it. I'll copy the Javadoc so this answer has more meat:
Named replacement in the URL path. Values are converted to string using
String.valueOf(Object)
. Values are used literally without URL encoding. See@Path
for URL encoding equivalent.
Use it like this:
@GET("/{path}") void example(@EncodedPath("path") String path, ..);