HTTP Methods
HTTP方法映射到资源的CRUD(创建、读取、更新和删除)操作,基本模式如下:
- HTTP GET:读取/列出/检索单个或资源集合。
- HTTP POST:新建资源。
- HTTP PUT:更新现有资源或资源集合。
- HTTP DELETE:删除资源或资源集合。
1、@Produces
用来指定将要返回给client端的数据标识类型(MIME)。@Produces可以作为class注释,也可以作为方法注释,方法的@Produces注释将会覆盖class的注释。(1)返回给client字符串类型(text/plain)
@Produces(MediaType.TEXT_PLAIN)(2)返回给client为json类型(application/json)
@Produces(MediaType.APPLICATION_JSON) @Consumes与@Produces相反,用来指定可以接受client发送过来的MIME类型,同样可以用于class或者method,也可以指定多个MIME类型,一般用于@PUT,@post(1)接受client参数为字符串类型
@Consumes(MediaType.TEXT_PLAIN) (2)接受clent参数为json类型
@Consumes(MediaType.APPLICATION_JSON) @PathParam
@GET
@Path("{username"})
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@PathParam("username") String userName) {
...
} @QueryParam@GET
@Path("/user")
@Produces("text/plain")
public User getUser(@QueryParam("name") String name,
@QueryParam("age") int age) {
...
} @DefaultValue
@GET
@Path("/user")
@Produces("text/plain")
public User getUser(@QueryParam("name") String name,
@DefaultValue("26") @QueryParam("age") int age) {
...
} @FormParam
@POST
@Consumes("application/x-www-form-urlencoded")
public void post(@FormParam("name") String name) {
// Store the message
}@BeanParam
@POST
@Consumes("application/x-www-form-urlencoded")
public void update(@BeanParam User user) {
// Store the user data
}
本文详细介绍了HTTP方法如何对应于CRUD操作,并解释了如何使用@Produces和@Consumes注解来处理不同类型的MIME数据。此外还列举了多种请求参数注解的用法,如@PathParam、@QueryParam等。

838

被折叠的 条评论
为什么被折叠?



