一、若希望一个Java类能够处理REST请求,则这个类必须至少添加一个@Path("/")的annotation;对于方法,这个annotation是可选的,如果不添加,则继承类的定义。
二、一个方法上只有添加了某个Http Method的annotation,例如@GET,才有资格处理请求。
三、@Path里的值可以是一个复杂的表达式,例如@Path("{id}") ,其中 {id}表达式代码了一个模板参数;一个模板参数是一个定义在@Path里的通配符,它以 { 开始,中间是一堆字母和数字的混合串(不能包含 / 字符),以 } 结尾。又如: @Path("{firstName}-{lastName}") .
四、@Path也支持正则表达式,例如: @Path("{id: \\d+}") ,其中 \\d+ 表示一个数字。格式为: A : B
五、可以自定http method的annotation,具体参考已经有的实现,例如@GET的实现:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod(HttpMethod.GET)
public @interface GET {
}
其中HttpMethod的实现为:
@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HttpMethod {
/**
* HTTP GET method
*/
public static final String GET="GET";
/**
* HTTP POST method
*/
public static final String POST="POST";
/**
* HTTP PUT method
*/
public static final String PUT="PUT";
/**
* HTTP DELETE method
*/
public static final String DELETE="DELETE";
/**
* HTTP HEAD method
*/
public static final String HEAD="HEAD";
/**
* HTTP OPTIONS method
*/
public static final String OPTIONS="OPTIONS";
/**
* Specifies the name of a HTTP method. E.g. "GET".
*/
String value();
}
因此,可以如下自定义新的http 请求方法:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("LOCK")
public @interface LOCK {
}
注:请不要试图重写HttpMethod annotation。
六、Path的优先级规则
- 首先检查匹配的字符的个数,匹配的个数越多越优先
- 其次检查内嵌的模板表达式的个数,个数越多越优先
- 最后是非缺省模板表达式的个数;缺省模板即未定义正则表达式的模板
例如以下顺利:
1 /customers/{id}/{name}/address
2 /customers/{id : .+}/address
3 /customers/{id}/address
4 /customers/{id : .+}
如果以上匹配不能解决你的问题,那多半是因为你的设计过于复杂,需要重新审视。
七、允许、保留和需要转意的字符
- 所以有 a-z、A-Z、0-9 都被允许
- 所有: _-!.~'()* 都被允许
- 所有: ,;:$&+=?/[]@ 都是保留字符 (需要转意)
- 所有其他字符需要用 % 进行转意;格式为 %HH (%加两个十六进制数)
注:如果Path中uri的表达式包含有需要转意的字符,JAX-RS会自动进行转意;否则会视之为已经进行了URL的encoding。
八、Matrix Param
Matrix Param是一个嵌在URI字符串中的name-value对,例如:
http://example.cars.com/mercedes/e55;color=black/2006
Matrix Param用于修饰特定的片段,如上修饰e55片段;不同于Query Param,用于修饰整个URI资源。
Matrix Param不出现在@Path的表达式中,例如以上URI对应的@Path的表达式可能是:@Path("/e55/{year}")。
但是可以用@MatrixParam annotation来取得Matrix Param的值,例如:@MatrixParam("color")
九、Subresource Locators
Subresources Locators是指一个指定了@Path annotation,但未指定HttpMethod的annotation,例如@GET,的Java方法。这个方法返回一个含有JAX-RS annotation的REST Service对象,这个对象知道怎么去分发剩余的请求。
例如:
@Path("/customers")
public class CustomerDatabaseResource {
@Path("{database}-db")
public CustomerResource getDatabase(@PathParam("database") String db) {
// find the instance based on the db parameter
CustomerResource resource = locateCustomerResource(db);
return resource;
}
protected CustomerResource locateCustomerResource(String db) {
...
}
}
其中CustomerResource可能是:
public class CustomerResource {
private Map<Integer, Customer> customerDB =
new ConcurrentHashMap<Integer, Customer>();
private AtomicInteger idCounter = new AtomicInteger();
@POST
@Consumes("application/xml")
public Response createCustomer(InputStream is) {
...
}
@GET
@Path("{id}")
@Produces("application/xml")
public StreamingOutput getCustomer(@PathParam("id") int id) {
...
}
@PUT
@Path("{id}")
@Consumes("application/xml")
public void updateCustomer(@PathParam("id") int id, InputStream is) {
...
}
}
因为CustomerResource 并不打算做一个Service暴露,所以在类上没有添加@Path的annotation。
十、完全动态分发
所谓完全分发,就是实现类可以是任意类,例如上面的CustomerDatabaseResource的getDatabase()方法的实现可以改为:
@Path("/customers")
public class CustomerDatabaseResource {
protected CustomerResource europe = new CustomerResource();
protected FirstLastCustomerResource northamerica =
new FirstLastCustomerResource();
@Path("{database}-db")
public Object getDatabase(@PathParam("database") String db) {
if (db.equals("europe")) {
return europe;
}
else if (db.equals("northamerica")) {
return northamerica;
}
else return null;
}
}
这里返回值是一个Object,已经不再局限于CustomerResource类了。JAX-RS会检查这个实例,以决定怎么分发请求。
分享到:
相关推荐
[奥莱理] RESTful Java 开发 (Jax-RS 实现) (英文版) [奥莱理] RESTful Java with Jax-RS (E-Book) ☆ 图书概要:☆ Learn how to design and develop distributed web services in Java using RESTful ...
The RESTEasy workbook that follows provides step-by-step instructions for installing, configuring, and running several working JAX-RS examples, using the JBoss RESTEasy implementation of JAX-RS 2.0. ...
使用到的外部jar包有(必须的部分,需要加到Web容器中) •neethi-3.0.2.jar •jsr311-api-1.1.1.jar •cxf-bundle-2.6.0.jar 使用到的外部jar包有(可选的部分,当且仅当作为一个独立的application运行时) ...
赠送jar包:jackson-jaxrs-json-provider-2.7.8.jar; 赠送原API文档:jackson-jaxrs-json-provider-2.7.8-javadoc.jar; 赠送源代码:jackson-jaxrs-json-provider-2.7.8-sources.jar; 赠送Maven依赖信息文件:...
javax.xml.ws.Service 报错需要的包,亲测可以用,直接下载在ide里buildpath一下就可以,四个jar包 ,整合了其他的jar所以配置简单
JAX-RS(JSR 311)指的是Java API for RESTful Web Services,Roy Fielding也参与了JAX-RS的制订,他在自己的博士论文中定义了REST。对于那些想要构建RESTful Web Services的开发者来说,JAX-RS给出了不同于JAX-WS...
概述这是一个多模块项目,包含用于以下数据格式的基于Jackson的JAX-RS提供程序:(二进制JSON) (另一种二进制JSON) 提供程序实现特定数据格式的JAX-RS MessageBodyReader和MessageBodyWriter处理程序。...
关于JAX-RS的注解详细,本人通过网络及实际应用收集的资料
使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的...
jax-rs 2.1规范最终版,英文原版。jax-rs规范定义了java平台Rest调用风格的WebService接口。
srb4j, Simple RESTFul backend for Java, 一个JAX-RS服务端代码脚手架(就像AppFuse之于Webapp),基于OAuth2提供了完整的注册、登录、密码恢复等功能。 它本质是一个Maven Archetype, 所以你可以用它快速生成一个...
JAX-RS REST helloworld 项目源码 下载后引入eclipse jee 启动tomcat可直接访问 访问路径为http://localhost:8080/AndroidJAX-RS/jaxrs/helloworld/html
restlet_jax-rs列子 可直接运行 在我的博客里有一些描述。希望能给大家提供点帮助
JAX-RS-FileUpload-Jersey-Example
JAX-RS+spring
web service rest(jax-rs)整合完整的Jax-rs组件包,内含2个版本,分别是1.0和1.4版本。
RESTful Java with JAX-RS 2.0.pdf
JAX-RS 性能 比较每个请求和单例 JAX-RS 资源类的性能。 请参阅。 该项目包括 4 个资源类:2 个每个请求范围和 2 个单例。 两个只是返回一个查询参数。 在另外两个资源中,我们将通过@Context 注入 UriInfo。 上...
jax-rs官方实例,对大家学习restful开发会有很大启发。