fly*_*yer 2 java rest web-services
我使用RESTful技术进行了很好的"骑行".我正在使用这样的Hello.java资源:
@Path("/hello")
public class Hello {
... /* GET/PUT/POST */
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以通过路径访问我的资源http://my.host/res/hello.我想更加努力地"骑"RESTful.拥有这一条资源路径有点无聊.
问题
我想有一个动态创建的资源,如下所示:
http://my.host/res/hellohttp://my.host/res/hello/1http://my.host/res/hello/2http://my.host/res/hello/999为每个人创建.java资源没有意义@Path("/hello/1") ... @Path("/hello/999").对?可能这个子资源列表可能更大或动态地随时间变化.解决方案是什么?
谢谢.
您可以@Path在Resource类中的方法上使用注释.
@Path("/hello")
public class Hello {
... /* GET/PUT/POST */
@GET
@Path("{id}")
public String myMethod(@PathParam("id") String id) {...}
}
Run Code Online (Sandbox Code Playgroud)
路径将连接在一起,以便匹配/hello/13.它{id}是输入的实际值的占位符,可以使用它来检索@PathParam.在前面的URI中,String id将具有该值13.
小智 6
您将不得不为REST URI使用PathParam功能.http://docs.oracle.com/javaee/6/api/javax/ws/rs/PathParam.html
@Path("/hello/{id}")
public class Hello {
}
Run Code Online (Sandbox Code Playgroud)