具体api解释可以在jax-rs中查找位于jee文档中:
http://docs.oracle.com/javaee/7/api/
基本注解:
@ApplicationPath ---定义所有URI的基本路径
@Path,标注资源类或方法的相对路径
@GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型
@Produces,标注返回的MIME媒体类型
@Consumes,标注可接受请求的MIME媒体类型
注入类的annotation
@PathParam: 从URI模板参数中提取数据
@MatrixParam:从URI中提取Matrix参数
请求路径:GET /cars/mercedes/e55;color=black/2006。则 make是mercedes;model是e55;year是2006;color是black。@Path("/cars/{make}")
public class CarResource {
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") PathSegment car,
@PathParam("year") String year) {
String carColor = car.getMatrixParameters().getFirst("color");
...
}
}
可以直接使用@MatrixParam去获取值,这样来得更直接、简洁,例如:
@Path("/{make}")
public class CarResource {
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") String model,
@MatrixParam("color") String color) {
...
}
}
不过如果Path中含有多个同名的MatrixParam,则还是需要使用PathSegment来获取,例如:GET /mercedes/e55;color=black/2006/interior;color=tan
@QueryParam:从URI中提取查询参数
请求路径:GET /customers?start=0&size=10@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(@QueryParam("start") int start,
@QueryParam("size") int size) {
...
}
}
@FormParam:提取Post Form参数
FormParam用于提取POST请求中的Form参数,其中Content-Type被假设为application/x-www-formurlencoded。例如有以下Form请求
<FORM action="http://example.com/customers" method="post">
<P>
First name: <INPUT type="text" name="firstname"><BR>
Last name: <INPUT type="text" name="lastname"><BR>
<INPUT type="submit" value="Send">
</P>
</FORM>
可以如下取值:
@Path("/customers")
public class CustomerResource {
@POST
public void createCustomer(@FormParam("firstname") String first,
@FormParam("lastname") String last) {
...
}
}
@HeaderParam:提取HTTP请求头信息
@Path("/myservice")public class MyService {
@GET
@Produces("text/html")
public String get(@HeaderParam("Referer") String referer) {
...
}
}
@CookieParam:提取客户设置的cookie的信息
@Path("/myservice")public class MyService {
@GET
@Produces("text/html")
public String get(@CookieParam("customerId") int custId) {
...
}
}
这里注入了的是一个cookie的值(customerId为客户端cookie的key),如果想取得更多的信息,而不仅仅是基本值,则可以直接注入javax.ws.rs.core.Cookie对象
@Path("/myservice")
public class MyService {
@GET
@Produces("text/html")
public String get(@CookieParam("customerId") Cookie custId) {
...
}
}
@Context:通用的注入annotation,允许注入各种帮助或者信息对象
例如:
使用相应注解:
@Path("/{make}")
public class CarResource {
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@PathParam("make") String make,
@PathParam("model") String model,
@MatrixParam("color") String color) {
...
}
}
使用通用注解,然后获取对应的值
@Path("/cars/{make}")
public class CarResource {
@GET
@Path("/{model}/{year}")
@Produces("image/jpeg")
public Jpeg getPicture(@Context UriInfo info) {
String make = info.getPathParameters().getFirst("make");
PathSegment model = info.getPathSegments().get(1);
String color = model.getMatrixParameters().getFirst("color");
...
}
}
扩展注解:
@DefaultValue 可以给某个请求参数定义缺省值,当client的请求中未包含此参数,则缺省参数值将被使用
@Path("/customers")
public class CustomerResource {
@GET
@Produces("application/xml")
public String getCustomers(@DefaultValue("0") @QueryParam("start") int start,
@DefaultValue("10") @QueryParam("size") int size) {
...
}
}
@Encoded 用来告诉JAX-RS,不需要自动解码,直接使用编码后的请求值
@GET@Produces("application/xml")
public String get(@Encoded @QueryParam("something") String str) {
...
}