RESTful 中创建"资源"
RESTful 中所有的请求都是由资源来处理的。JAX-RS API实现的资源就是一个Java Class,这个Class被Annotated了
一个或多个Annotations; 使用JAX-RS实现的RESTful Web service是一个root resource class, 这个root resource class
?service expose后的访问的入口,该root resource class可以自己处理request,也可以由其sub-resource来处理request;
?即RESTfule 中有两种resource type: root resource class,sub-resource
?
1. Basic JAX-RS annotations
???? (1) URI template syntax: URI template syntax
???? (2) Specifying HTTP verbs
???? javax.ws.rs.DELETE specifies that the method maps to a DELETE.???? javax.ws.rs.GET specifies that the method maps to a GET.???? javax.ws.rs.POST specifies that the method maps to a POST.???? javax.ws.rs.PUT specifies that the method maps to a PUT.???? javax.ws.rs.HEAD specifies that the method maps to a HEAD.???? (3) Root resource classes
???? Class被用于Root Class需满足一下条件:???? Class 必须被标注@Path注解;???? Class必须有一个公共的构造函数用于运行期间调用;???? Class中的Method至少有一个被标注HTTP verb或@Path???? 例如:
@Path("/customerservice/")public class CustomerService{@Path("/orders/{orderId}/")public Order processOrder(@PathParam("orderId") String orderId){}}public class Order{@GETpublic Order getOrder(@PathParam("orderId") String orderId){}@PUTpublic Order updateOrder(@PathParam("orderId") String orderId,Order order){}}???? (7) Resource selection method
???? JAX-RS选择resource method 算法被分成三个阶段:
???? A. Determine the root resource class;
???? B. Determine the object will handle the request;
???? C. Select the resource method that will handle the request.
???? 1. Selecting from multiple resource classes
???????? 当有多个resource匹配reqauest URI时,resource class优先于sub-resource;当有多个resource class时按照
???????? 下面的条件匹配:
???????? (1) Prefer the resource with the most literal characters in its URI template;
???????? (2) Prefer the resource with the most variables in its URI template
???????? (3) Prefer the resource with the most variables containing regular expressions.
????? 2.Selecting from multiple resource methods
???????? 按照下面的条件进行匹配:
???????? (1) Prefer resource methods over sub-resources;
???????? (2) Prefer sub-resource methods over sub-resource locaters;
???????? (3) Prefer methods that use the most specific values in the @Consumes annotation and the @Produces
???????? annotation; 例如:@Consumes(text/xml) > @Consumes(text/*) > @Consumes(*/*)
???????? (4) Prefer methods that most closely match the content type of the request body entity; HTTP Content-Type
???????? property.
???????? (5) Prefer methods that most closely match the content type accepted as a response; HTTP Accept property.
????? 3.Customizing the selection process
???????? 实现org.apache.cxf.jaxrs.ext.ResourceComparator.