Java Jersey:获取方法的URL
我想获取特定方法的URI,而不需要对其进行“硬编码” 我尝试了Java Jersey:获取方法的URL,java,jersey,uri,builder,Java,Jersey,Uri,Builder,我想获取特定方法的URI,而不需要对其进行“硬编码” 我尝试了UriBuilder.fromMethod,但它只生成在该方法的@Path注释中指定的URI,它没有考虑它所在的资源类的@Path 举个例子,这里是课堂 @Path("/v0/app") public class AppController { @Path("/{app-id}") public String getApp(@PathParam("app-id") int appid) { // ..
UriBuilder.fromMethod
,但它只生成在该方法的@Path
注释中指定的URI,它没有考虑它所在的资源类的@Path
举个例子,这里是课堂
@Path("/v0/app")
public class AppController {
@Path("/{app-id}")
public String getApp(@PathParam("app-id") int appid) {
// ...
}
}
我想获取getApp
方法的URL,例如/v0/app/100
更新:
我想从方法而不是
getApp
获取URL。如果使用UriBuilder.fromResource
,则可以使用路径(类资源,字符串方法)
不确定它为什么不能与fromMethod
一起使用
这是一个测试用例
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());
}
}
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());
}
}