Jersey常用注解

本文介绍了Jersey框架中常用的RESTful API注解,包括@ApplicationPath、@GET、@POST、@PUT、@DELETE等HTTP方法注解,以及@PathParam、@QueryParam、@FormParam、@BeanParam等参数处理注解。还介绍了如何使用@Context、@Consumes和@Produces注解来处理复杂的请求和响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Charpter 2-2 Jersey常用注解

@ApplicationPath

该注解用于声明Jersey的全局配置类,也即整个Jersey框架的程序入口。该类需要满足下面条件。

  1. @ApplicationPath注解该类,并且指定该类对应的路径
  2. 继承 org.glassfish.jersey.server.ResourceConfig 类
  3. 在该类中配置Jersey的配置,例如声明资源包路径,配置拦截器等。
@ApplicationPath("/")
public class Application extends ResourceConfig{
    public Application() {
        /*声明资源包所在位置
        * */
        packages("cn.lx.resource");
    }
}

HTTP Method

在Jersey框架里,HTTP方法对应资源的不同操作CRUD,基本的使用方式如下

@GET

用于读取、列出、检索单个或者资源集合。

@POST

用于新建资源。

@PUT

用于更新资源

@DELETE

用于删除资源

参数封装方式

@PathParam

PathParam 可以用于获取URI中根据Restful规则设定的参数。
例如:

@GET
@Path("get/{id}")
public Student getById(@PathParam("id") String id){
    Student student = map.get(id);
    return student;
}

当浏览器或者客户端请求http://localhost/student/get/1 ,id的值是1

@QueryParam

QueryParam 注解用于获取GET请求中的查询参数
例如:

@GET
@Path("/get")
public Student get(@QueryParam("id") String id){
    Student student = map.get(id);
    return student;
}

浏览器或客户端请求http://localhost/student/get?id=1时,id的值为1,可以在程序中指定默认值,使用@DefaultValue注解。

@DefaultValue("1") @QueryParam("id") String id

当客户端没有传递参数时,id的默认值为1,如果传递参数,则为客户端传递的值。

@FormParam

FormParam 就是Post请求中,通过表单提交传递的参数。

@POST
@Path("/add")
public String add(@FormParam("id") String id,
                  @FormParam("name") String name,
                  @FormParam("age") Integer age){
    Student student = new Student();
    student.setId(id);
    student.setName(name);
    student.setAge(age);
    map.put(id,student);
    return "success";
}
@BeanParam

顾名思义,BeanParam的作用就是将参数封装到Bean对象,在Jersey框架中如果传递的参数是Json格式,会自动将数据封装到Bean对象中,但如果是POST、PUT、GET请求的参数封装到Bean对象中时,就需要使用到BeanParam注解。

@Path("/addByBean")
public String addByBean(@BeanParam  Student student){
    map.put(student.getId(),student);
    return "success";
}

如上代码所示,如果需要向Student中封装数据,需要对Student类进行处理,当为GET请求时。

public class Student {
    @QueryParam("id")
    private String id;
    @QueryParam("name")
    private String name;
    @QueryParam("age")
    private Integer age;
}

当客户端请求的路径是/addByBean?id=1&name=1&age=1 就会将参数自动封装为Student对象。
当为PUT或者POST请求时

public class Student {
    @FormParam("id")
    private String id;
    @FormParam("name")
    private String name;
    @FormParam("age")
    private Integer age;
}
@Context

因为传递参数形式的多变性,参数可能比我们上面介绍的形式更为复杂,这时可以使用Context注解,来获取更加复杂的数据。

@GET
public String get(@Context UriInfo ui) {
    MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
    MultivaluedMap<String, String> pathParams = ui.getPathParameters();
}

同样还可以通过 @Context 注释获取 ServletConfig 、 ServletContext 、 HttpServletRequest 、 HttpServletResponse 和 HttpHeaders 等,如下:

@Path("/")
public class Resource {  

  @Context  HttpServletRequest req;

  @Context  ServletConfig servletConfig;

  @Context  ServletContext servletContext;

  @GET  
  public String get(@Context HttpHeaders hh) {
    MultivaluedMap<String, String> headerParams = hh.getRequestHeaders();
    Map<String, Cookie> pathParams = hh.getCookies();
  }
}

@Consumes

Consumes注解可以使用在方法或者类上,表示请求参数的格式。常用的如下:
@Consumes(MediaType.APPLICATION_JSON) 表示JSON数据
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 表示表单数据
@Consumes(MediaType.APPLICATION_XML) 表示XML数据

@Produces

Produces注解和Consumes注解相对应,同样使用在方法或者类上,用于表示响应数据的类型。同样的常用类型如下。
@Produces(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_XML)

上面介绍的注解是我们在开发过程中使用较为多的,更多的请参加官方文档。
Jersey官方手册
JerseyApi

Jersey系列相关源码请访问:Jersey_learing项目

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值