上一篇中的 HelloJersey 增加功能,使用 POST 傳資料,僅添加新增部份,至於測試需要 PostMan 、解釋等等就跳過囉!
package com.test.jersey;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
@Path("/hello") //提供服務的 URL
public class HelloJersey {
@GET //對應的要求方法:可以改為 GET, POST, PUT, DELETE
public String sayHello() {
return "Hello, Jersey";
}
@Path("addcust") //這是為了與 sayHello() 區分增加的 URL
@POST // 改為 POST 傳資料
@Produces("text/plain") //傳遞資料格式ex:@Produces("text/html")
//@QueryParam 來自於 URL 的查詢參數、這裡需加不然會抓不到資料
public String customerAdd(@QueryParam("cid")String id ){
return String.format("%s ADDOK!!", id);
}
}