路徑參數將傳入的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