Charpter 2-2 Jersey常用注解
@ApplicationPath
该注解用于声明Jersey的全局配置类,也即整个Jersey框架的程序入口。该类需要满足下面条件。
- @ApplicationPath注解该类,并且指定该类对应的路径
- 继承 org.glassfish.jersey.server.ResourceConfig 类
- 在该类中配置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项目