JAX-RS 2.0:REST 式 Web 服务 API 中新增的和值得关注的功能_第1页
JAX-RS 2.0:REST 式 Web 服务 API 中新增的和值得关注的功能_第2页
JAX-RS 2.0:REST 式 Web 服务 API 中新增的和值得关注的功能_第3页
JAX-RS 2.0:REST 式 Web 服务 API 中新增的和值得关注的功能_第4页
JAX-RS 2.0:REST 式 Web 服务 API 中新增的和值得关注的功能_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级1版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级2JAX-RS 2.0:REST 式式 Web 服务服务 API 中新增的和值得关中新增的和值得关注的功能注的功能John ClinganJava EE 和 GlassFish 产品经理版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级3版权所有 2012,Oracle 和/或其关联公司。保留所有权利。JAX-RS 2.0 中的新中的

2、新增功能增功能JAX-RS 回顾客户端 API通用配置异步处理过滤器/拦截器超媒体支持服务器端内容协商版权所有 2012,Oracle 和/或其关联公司。保留所有权利。4JAX-RS 用于用于 REST 式服务的式服务的 Java API基于 POJO 的资源类以 HTTP 为中心的编程模型实体格式独立性容器独立性包括在 Java EE 中注解驱动的标准API,用于帮助开发人员使用 Java 构建 REST 式 Web 服务和客户端版权所有 2012,Oracle 和/或其关联公司。保留所有权利。5JAX-RS 示例示例 .POST Path(/withdrawal)Consumes(text

3、/plain) Produces(application/json)public Money withdraw(PathParam(card) String card,QueryParam(pin) String pin, String amount) return getMoney(card, pin, amount); 版权所有 2012,Oracle 和/或其关联公司。保留所有权利。6JAX-RS 注解注解方法方法注解注解目的目的GETGET读取,可能缓存POSTPOST在没有已知 ID 的情况下更新或创建PUTPUT在有已知 ID 的情况下更新或创建DELETEDELETE删除HEAD

4、HEAD没有响应的 GETOPTIONSOptions支持的方法版权所有 2012,Oracle 和/或其关联公司。保留所有权利。7JAX-RS 注解(续)注解(续)注解注解目的目的PathParam绑定来自 URI 的值,例如PathParam(“id”)QueryParam绑定查询名称的值/查询值,例如QueryParam(“name”)CookieParam绑定 cookie 的值,例如CookieParam(“JSESSIONID”)HeaderParam绑定 HTTP 标头的值,例如HeaderParam(“Accept”)FormParam绑定 HTML 表单的值,例如FormPa

5、ram(“name”)MatrixParam绑定矩阵参数的值,例如MatrixParam(“name”)版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级8JAX RS 2.0 客户端客户端 API版权所有 2012,Oracle 和/或其关联公司。保留所有权利。9客户端客户端 APIHTTP 客户端库太低级利用 JAX-RS 1.x API 中的提供商/概念主要实现引入的专用 API动机版权所有 2012,Oracle 和/或其关联公司。保留所有权利。10客户端客户端 API/ Get instance of ClientClient c

6、lient = ClientBuilder.newClient();/ Get account balanceString bal = client.target(http:/./atm/cardId/balance) .resolveTemplate(cardId, 111122223333) .queryParam(pin, 9876) .request(text/plain).get(String.class);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。11客户端客户端 API/ Withdraw some moneyMoneymoney =client.targ

7、et(http:/./atm/cardId/withdrawal) .resolveTemplate(cardId, 111122223333) .queryParam(pin, 9876) .request(application/json) .post(text(50.0), Money.class);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。12客户端客户端 APIInvocationinvocation1 = client.target(http:/./atm/cardId/balance).request(“text/plain”).buildGet();In

8、vocationinvocation2 = client.target(http:/./atm/cardId/withdraw) .request(application/json) .buildPost(text(50.0);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。13客户端客户端 APICollection invocations =Arrays.asList(inv1, inv2);Collection responses = Collections.transform(invocations, new F() public Responseapply(Invo

9、cationinvocation) return invocation.invoke(); );版权所有 2012,Oracle 和/或其关联公司。保留所有权利。14客户端客户端 API/ Create client and register MyProvider1Client client = ClientBuilder.newClient();client.register(MyProvider1.class);/ Create atm target; inherits MyProvider1WebTargetatm = client.target(http:/./atm);/ Regis

10、ter MyProvider2atm.register(MyProvider2.class);/ Create balance target; inherits MyProvider1, MyProvider2WebTarget balance = atm.path(”cardId/balance);/ Register MyProvider3balance.register(MyProvider3.class);版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级15JAX RS 2.0通用配置通用配置版权所有 2012,Oracle 和/

11、或其关联公司。保留所有权利。16通用配置通用配置 动机动机客户端客户端客户端 .register(JsonMessageBodyReader.class) .register(JsonMessageBodyWriter.class) .register(JsonpInterceptor.class) .property(“”, “callback”) .property(“jsonp.callback.queryParam”, “true”) .版权所有 2012,Oracle 和/或其关联公司。保留所有权利。17通用配置通用配置 动机动机服务器端publ

12、ic class MyApp extends javax.ws.rs.core.Application public SetClass getClasses() SetClass classes = new HashSet(); . classes.add(JsonMessageBodyReader.class); classes.add(JsonMessageBodyWriter.class); classes.add(JsonpInterceptor.class); . return classes; 版权所有 2012,Oracle 和/或其关联公司。保留所有权利。18通用配置通用配置

13、解决方案解决方案客户端客户端客户端 .register(JsonMessageBodyReader.class) .register(JsonMessageBodyWriter.class) .register(JsonpInterceptor.class) .property(“”, “callback”) .property(“jsonp.callback.queryParam”, “true”) .JsonFeaturejf= new JsonFeature().enableCallbackQueryParam();client.register(j

14、f);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。19通用配置通用配置 解决方案解决方案服务器端public SetClassgetClasses() .classes.add(JsonMessageBodyReader.class);classes.add(JsonMessageBodyWriter.class);classes.add(JsonpInterceptor.class); .public SetClassgetClasses() .classes.add(JsonFeature.class); .版权所有 2012,Oracle 和/或其关联公司。保留所有

15、权利。20通用配置通用配置public interface Configurable Configuration getConfiguration(); Configurable property(String name, Object value); Configurable register(.);public interface Configuration Set getClasses(); Map getContracts(Class componentClass); Set getInstances(); Map getProperties(); Object getProperty

16、(String name); Collection getPropertyNames(); boolean isEnabled(Feature feature); boolean isRegistered(Object component); .版权所有 2012,Oracle 和/或其关联公司。保留所有权利。21通用配置通用配置public interface Feature boolean configure(FeatureContext context);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。22Feature 示例示例public void JsonFeat

17、ure implements Feature public boolean configure(FeatureContext context) context.register(JsonMessageBodyReader.class) .register(JsonMessageBodyWriter.class) .register(JsonpInterceptor.class) .property(CALLBACK_NAME, calbackName) .property(USE_QUERY_PARAM, useQueryParam); return true; 版权所有 2012,Oracl

18、e 和/或其关联公司。保留所有权利。23动态动态 Feature仅服务器端public interface DynamicFeature void configure(ResourceInfo ri, FeatureContext context);public interface ResourceInfo Method getResourceMethod(); Class getResourceClass();版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级24JAX RS 2.0异步处理异步处理版权所有 2012,Oracle 和/或

19、其关联公司。保留所有权利。25异步处理异步处理服务器 API分流 I/O 容器线程高效的异步事件处理利用 Servlet 3.x 异步支持(如果可用)客户端 API异步的请求调用 API版权所有 2012,Oracle 和/或其关联公司。保留所有权利。26异步处理异步处理StatelessPath(/async/longRunning)public class MyResource GET Asynchronous public void longRunningOp(SuspendedAsyncResponse ar) ar.setTimeoutHandler(new MyTimoutHand

20、ler(); ar.setTimeout(15, SECONDS); final String result = executeLongRunningOperation(); ar.resume(result); 版权所有 2012,Oracle 和/或其关联公司。保留所有权利。27异步处理:服务器端异步处理:服务器端public interface AsyncResponse public void resume(Object/Throwable response); public void cancel(); public void cancel(int/Date retryAfter);

21、 public boolean isSuspended(); public boolean isCancelled(); public boolean isDone(); public void setTimeout(long time, TimeUnit unit); public void setTimeoutHandler(TimeoutHandler handler); public CollectionClass register(Class callback); public MapClass,CollectionClass register(Class callback, Cla

22、ss. callbacks); public CollectionClass register(Object callback); public MapClass,CollectionClass register(Object callback, Object. callbacks);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。28异步处理:服务器端异步处理:服务器端Target(ElementType.PARAMETER)Retention(RetentionPolicy.RUNTIME)Documentedpublic interface Suspended publ

23、ic interface TimeoutHandler void handleTimeout(AsyncResponse asyncResponse);版权所有 2012,Oracle 和/或其关联公司。保留所有权利。29异步处理:服务器端异步处理:服务器端public interface CompletionCallback public void onComplete(Throwable throwable);public interface ConnectionCallback public void onDisconnect(AsyncResponse disconnected);版权

24、所有 2012,Oracle 和/或其关联公司。保留所有权利。30异步处理:客户端异步处理:客户端WebTarget target = client.target(http:/./balance”)/ Start async call and register callbackFuture handle = target.request().async().get( new InvocationCallback() void complete(String balance) void failed(InvocationException e) );/ After waiting for too

25、 longif (!handle.isDone() handle.cancel(true);版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级31JAX RS 2.0过滤器过滤器/拦截器拦截器版权所有 2012,Oracle 和/或其关联公司。保留所有权利。32过滤器和拦截器过滤器和拦截器定制 JAX-RS 请求/响应处理用例:日志记录、压缩、安全性,等等针对客户端和服务器 API 而引入替换现有的专用支持动机版权所有 2012,Oracle 和/或其关联公司。保留所有权利。33过滤器过滤器和拦截器和拦截器非包装过滤器链过滤器不直接调用链中

26、的下一个过滤器由 JAX-RS 运行时管理每个过滤器决定是继续还是中断链过滤每个传入/传出消息请求 请求ContainerRequestFilter, ClientRequestFilter响应 响应ContainerResponseFilter, ClientResponseFilter服务器端特性PreMatching、DynamicFeature版权所有 2012,Oracle 和/或其关联公司。保留所有权利。34过滤器过滤器和拦截器和拦截器日志记录过滤器示例public class RequestLoggingFilter implements ContainerRequestFilt

27、er Override public void filter(ContainerRequestContext requestContext) log(requestContext); / non-wrapping = returns without invoking the next filter .版权所有 2012,Oracle 和/或其关联公司。保留所有权利。35过滤器和过滤器和拦截器拦截器只有当发生实体处理时才会调用性能提升包装拦截器链每个拦截器都通过 ceed() 调用链中的下一个拦截器拦截实体提供者MessageBodyReader 拦截器ReaderInte

28、rceptor接口MessageBodyWriter 拦截器WriterInterceptor接口版权所有 2012,Oracle 和/或其关联公司。保留所有权利。36过滤器和过滤器和拦截器拦截器Gzip 读取器拦截器示例public class GzipInterceptor implements ReaderInterceptor Override Object aroundReadFrom(ReaderInterceptorContext ctx) InputStream old = ctx.getInputStream(); ctx.setInputStream(new GZIPInp

29、utStream(old);/ wrapping = invokes the next interceptorObject entity = ceed(); ctx.setInputStream(old); return entity; 版权所有 2012,Oracle 和/或其关联公司。保留所有权利。37过滤器过滤器和和拦截器拦截器应用程序请求过滤过滤器器过滤过滤器器网络传输响应过滤过滤器器过滤过滤器器写入器写入器拦截器拦截器MBWMBR写入器写入器拦截器拦截器读取器读取器拦截器拦截器读取器读取器拦截器拦截器版权所有 2012,Oracle 和/或其关联公司。保留所有权利。38

30、过滤器过滤器和和拦截器拦截器响应应用程序过滤过滤器器过滤过滤器器网络响应过滤过滤器器过滤过滤器器MBW写入器写入器拦截器拦截器写入器写入器拦截器拦截器过滤过滤器器过滤过滤器器请求请求读取器读取器拦截器拦截器MBR读取器读取器拦截器拦截器过滤过滤器器过滤过滤器器资源匹配PreMatching版权所有 2012,Oracle 和/或其关联公司。保留所有权利。39绑定和优先级绑定和优先级绑定将过滤器和拦截器与资源方法相关联服务器端概念优先级声明在执行链中的相对位置Priority(int priority)过滤器和拦截器共有的概念有作用域的绑定全局绑定静态NameBinding默认PreMatchi

31、ng动态DynamicFeature无版权所有 2012,Oracle 和/或其关联公司。保留所有权利。40绑定绑定NameBindingTarget(ElementType.TYPE, ElementType.METHOD)Retention(value = RetentionPolicy.RUNTIME)public interface Logged ProviderLoggedPriority(USER)public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter 版权所有 20

32、12,Oracle 和/或其关联公司。保留所有权利。41绑定绑定Path(/greet/name)Produces(text/plain)public class MyResourceClass Logged GET public String hello(PathParam(name) String name) return Hello + name; 版权所有 2012,Oracle 和/或其关联公司。保留所有权利。42DynamicFeature 示例示例仅服务器端public void SecurityFeature implements DynamicFeature public b

33、oolean configure(ResourceInfo ri, FeatureContext context) String roles = getRolesAllowed(ri); if (roles != null) context.register(new RolesAllowedFilter(roles); .版权所有 2012,Oracle 和/或其分支机构。保留所有权利。插入幻灯片 13 中的保护政策分类等级43JAX RS 2.0超媒体支持超媒体支持版权所有 2012,Oracle 和/或其关联公司。保留所有权利。44超媒体支持超媒体支持REST 原则标识符和链接HATEOA

34、S(超媒体作为应用程序状态的引擎)链接类型:结构性链接过渡性链接版权所有 2012,Oracle 和/或其关联公司。保留所有权利。45超媒体支持超媒体支持Link:; rel=ship,; rel=cancel .http:/./customers/11http:/./customers/11/address/1http:/./products/1112 . 版权所有 2012,Oracle 和/或其关联公司。保留所有权利。46超媒体超媒体Link 和 LinkBuilder 类RFC 5988:Web 链接支持 ResponseBuilder 和过滤器中的链接过渡性链接(标头)支持手动结构性

35、链接通过 Link.JaxbAdapter 和 Link.JaxbLink从客户端 API 中的链接创建资源目标版权所有 2012,Oracle 和/或其关联公司。保留所有权利。47超媒体超媒体/ Producer API (server-side)Link self= Link.fromMethod(MyResource.class, ”handleGet”).build();Link update= Link.fromMethod(MyResource.class, “handlePost”).rel(”update”).build();.Response res = Response.ok(order).link(http:

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论