Jersey: Enter Method URL
I want to get the URI for a specific method without "hardcoding".
I tried it UriBuilder.fromMethod
, but it only generates the URI given in the annotation @Path
for this method, it doesn't take into account @Path
the resource class it is in.
For example, here the class
@Path("/v0/app")
public class AppController {
@Path("/{app-id}")
public String getApp(@PathParam("app-id") int appid) {
// ...
}
}
I want to get the url for a method getApp
like /v0/app/100
.
UPDATE:
I want to get the url from a method other than getApp
source to share
It works if you use UriBuilder.fromResource
then add the method path withpath(Class resource, String method)
URI uri = UriBuilder
.fromResource(AppController.class)
.path(AppController.class, "getApp")
.resolveTemplate("app-id", 1)
.build();
Not sure why it doesn't work with fromMethod
.
Here's a test case
public class UriBuilderTest {
@Path("/v0/app")
public static class AppController {
@Path("/{app-id}")
public String getApp(@PathParam("app-id") int appid) {
return null;
}
}
@Test
public void testit() {
URI uri = UriBuilder
.fromResource(AppController.class)
.path(AppController.class, "getApp")
.resolveTemplate("app-id", 1)
.build();
assertEquals("/v0/app/1", uri.toASCIIString());
}
}
source to share