RESTful Web Service - JAX-RS Annotations

RESTful Web Service - JAX-RS Annotations - Contents:

Annotation

Package Detail/Import statement

@GET

import javax.ws.rs.GET;

@Produces

import javax.ws.rs.Produces;

@Path

import javax.ws.rs.Path;

@PathParam

import javax.ws.rs.PathParam;

@QueryParam

import javax.ws.rs.QueryParam;

@POST

import javax.ws.rs.POST;

@Consumes

import javax.ws.rs.Consumes;

@FormParam

import javax.ws.rs.FormParam;

@PUT

import javax.ws.rs.PUT;

@DELETE

import javax.ws.rs.DELETE;

REST follows one-to-one mapping between create, read, update, and delete (CRUD) operations and HTTP methods. 

· To create a resource on the server, use POST.

· To retrieve a resource, use GET.

· To change the state of a resource or to update it, use PUT.

· To remove or delete a resource, use DELETE.


@GET

Annotate your Get request methods with @GET.
@GETpublic String getHTML() {   ... }

@Produces

@Produces annotation specifies the type of output this method (or web service) will produce.
@GET
@Produces("application/xml") 
public Contact getXML() {   ... } 

@GET@Produces("application/json") 
public Contact getJSON() {   ... }

@Path

@Path annotation specify the URL path on which this method will be invoked.
@GET
@Produces("application/xml") 
@Path("xml/{firstName}") 
public Contact getXML() {   ... }

@PathParam

We can bind REST-style URL parameters to method arguments using @PathParam annotation as shown below.
@GET
@Produces("application/xml") 
@Path("xml/{firstName}") 
public Contact getXML(@PathParam("firstName") String firstName) 
{   
   Contact contact = contactService.findByFirstName(firstName); 
   return contact;
} 

@GET
@Produces("application/json") 
@Path("json/{firstName}") 
public Contact getJSON(@PathParam("firstName") String firstName) 
{   
    Contact contact = contactService.findByFirstName(firstName);
    return contact; 
}

@QueryParam

Request parameters in query string can be accessed using @QueryParam annotation as shown below.
@GET@Produces("application/json") 
@Path("json/companyList") 
public CompanyList getJSON(@QueryParam("start") int start, @QueryParam("limit") int limit)
{   
   CompanyList list = new CompanyList(companyService.listCompanies(start, limit));
   return list;
}

@POST

Annotate POST request methods with @POST.
@POST
@Consumes("application/json") 
@Produces("application/json") 
public RestResponse<Contact> create(Contact contact) { ... }

@Consumes

The @Consumes annotation is used to specify the MIME media types a REST resource can consume.
@PUT
@Consumes("application/json") 
@Produces("application/json") 
@Path("{contactId}") 
public RestResponse<Contact> update(Contact contact) { ... }

@FormParam

The REST resources will usually consume XML/JSON for the complete Entity Bean. Sometimes, you may want to read parameters sent in POST requests directly and you can do that using @FormParam annotation. GET Request query parameters can be accessed using @QueryParam annotation.


@POST
public String save(@FormParam("firstName") String firstName,@FormParam("lastName") String lastName)
{
 ...
}


@PUT

Annotate PUT request methods with @PUT.

@PUT
@Consumes("application/json")
@Produces("application/json") 
@Path("{contactId}") 
public RestResponse<Contact> update(Contact contact) 
{ 
... 
}


@DELETE

Annotate DELETE request methods with @DELETE.


@DELETE
@Produces("application/json")
@Path("{contactId}")
public RestResponse<Contact> delete(@PathParam("contactId") int contactId) 
{
 ... 
}

References

  1. Jersey JAX-RS Annotations: https://wikis.oracle.com/display/Jersey/Overview+of+JAX-RS+1.0+Features
参考地址: http://www.techferry.com/articles/RESTful-web-services-JAX-RS-annotations.html


转载于:https://my.oschina.net/boonya/blog/135075

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值