jax-rs的注解使用



具体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,允许注入各种帮助或者信息对象


所有的注入都可以通过通用的Context来注入到响应的rs服务类中然后获取相应的值
例如:
使用相应注解:
@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) {

                                                    ...

                                              }

  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值