Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey:获取方法的URL_Java_Jersey_Uri_Builder - Fatal编程技术网

Java Jersey:获取方法的URL

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) { // ..

我想获取特定方法的URI,而不需要对其进行“硬编码”

我尝试了
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());
    }
}