Skip to content

Instantly share code, notes, and snippets.

@ShawnSWu
Last active June 27, 2021 18:43
Show Gist options
  • Save ShawnSWu/bdbcf38a0aed457887e566089c0e1e7c to your computer and use it in GitHub Desktop.
Save ShawnSWu/bdbcf38a0aed457887e566089c0e1e7c to your computer and use it in GitHub Desktop.
JerseyExmaple
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public class ExampleAPI {
/*
@PATH, @GET, @POST, @PUT, @DELETE 這些就不解釋了,夠直覺了。
*/
@GET
@Path("getExamplePath")
public String getExample(){
...
}
@POST
@Path("postExamplePath")
public String postExample(){
...
}
@PUT
@Path("putExamplePath")
public String putExample(){
...
}
@DELETE
@Path("deleteExamplePath")
public String deleteExample(){
...
}
/*
@Consumes: 指定http請求的MIME type(e.g. json or xml)
@Produces: 指定http響應的MIME type(e.g. json or xml)
*/
@GET
@Path("/mimeTypeExample")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String mimeTypeExample() {
...
}
/*
@FormParam,顧名思義,從POST請求的表單中獲取資料
*/
@POST
@Path("/formParamExample")
public void formParamExample(@FormParam("name") String name) {
...
}
/*
@PathParam,獲得URI中指定的參數
ex. http://localhost/pathParamExample/myName
*/
@GET
@Path("/pathParamExample")
public void pathParamExample(@PathParam("name") String name) {
...
}
/*
@QueryParam,用於獲取GET請求中的查詢參數
ex. http://localhost/pathParamExample?name=myName
*/
@GET
@Path("/queryParamExample")
public void queryParamExample(@QueryParam("name") String name) {
...
}
/*
@DefaultValue,設定@QueryParam的預設值,當@QueryParam沒有收到值時,就使用此默認值
*/
@GET
@Path("/defaultParamExample")
public void defaultParamExample(@DefaultValue("default name")
@QueryParam("name") String name) {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment