1.@path
当我们希望一个类可以处理请求的时候,这个类必须至少添加一个@path("/")的annotation,如:
@Path(value = "/sample")
public class RESTSampleSource implements RESTSample {} 对于这个java类中的方法,@path这个注解是可选的,如果不写,则继承类的@path
而对于@path里面的值,可以有很多格式:
(1).@path("/xx/{xx}"),{xx}表示传递的参数,一个模板参数是一个定义在@Path里的通配符,它以 { 开始,中间是一堆字母和数字的混合串(不能包含 / 字符),以 } 结尾。又如: @Path("{firstName}-{lastName}")
@POST
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/queryUserInfo2/{id}")
public UserResponse queryUserInfo2(@PathParam("id") String id, @Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse) throws Exception{....} (2).
@Path也支持正则表达式,例如: @Path("{id: \\d+}") ,其中 \\d+ 表示一个数字。格式为: A : B
2.@Method:
@POST,@GET :表示可以接受什么类型的请求
3.@Produces:类或者方法返回的MIME数据类型,如
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
上述表示可以返回xml和json两种格式的响应内容
当然还有其他几种:
(1)@Produces("text/plain") 文本类型
(2)@Produces("text/html") Html类型
(3)@Produces({"application/xml"}) Xml类型
(4)@Produces({ "application/json"}) Json类型
4.@PathParam,值是@paht里面需要的参数,如:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/request/{param}")
public String doRequest(@PathParam("param") String param){....}5.@Context:使用该注释注入上下文对象,比如 Request、Response、UriInfo、ServletContext 等,如:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/request/{param}")
public String doRequest(@PathParam("param") String param,
@Context HttpServletRequest servletRequest,
@Context HttpServletResponse servletResponse) {....}6.Produces:响应支持多个 MIME 类型。在本例和上一个示例中,APPLICATION/XML 将是默认的 MIME 类型,如:
@GET
@Produces(MediaType.TEXT_PLAIN)
public String doGet() {
return "this is get rest request";
}
2060



被折叠的 条评论
为什么被折叠?



