路径参数将传入的URL和路径的各部分作为参数进行匹配。通过将{name}包含在@Path注释中,资源 可以稍后访问URI的匹配部分,并将路径参数与 对应的“名称”相关联。路径参数使请求 URL的部分作为参数,这可以将请求参数 信息嵌入到简单URL中。
@Path("/books/{bookid}")
public class BookResource {
@GET
public Response invokeWithBookId(@PathParam("bookid") String bookId) {
/* get the info for bookId */
return Response.ok(/* some entity here */).build();
}
@GET
@Path("{language}")
public Response invokeWithBookIdAndLanguage(@PathParam("bookid") String bookId, @PathParam("language") String language) {
/* get the info for bookId */
return Response.ok(/* some entity here */).build();
}
}
在你休息的代码参数Info
取值从@Path("/songs/{json}")
但你必须指定@Path("/songs/")
所以json
永远是null
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
MediaType.TEXT_XML })
@Path("/songs/")
public Room AddSong(@PathParam("json") String Info) {
Song newSong = new newSong();
newSong.addSong(Info);
return newSong;
}
你不喜欢这样,然后一切将罚款:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
MediaType.TEXT_XML })
@Path("/songs/{json}")
public Room AddSong(@PathParam("json") String Info) {
Song newSong = new newSong();
newSong.addSong(Info);
return newSong;
}
欲了解更多信息请参考JAX-RS Parameters