Upload
javatwo2011
View
9.464
Download
9
Embed Size (px)
DESCRIPTION
大綱1. RESTful Services 簡介2. 現有的 JAX-RS (JSR-311) 與 Implementation3. JAX-RS Service 端的使用方式4. JAX-RS Client 端的支援5. Security 的考量6. 未來 Java EE 7 的 JAX-RS 2.0 (JSR-339)介紹這個 Session 主要會以 NetBeans 7.0 搭配 GlassFish 3.1 裡面的 Jersey,為大家介紹JAX-RS 1.1 在 Server 端與 Client 端的應用方式,以及未來 JAX-RS 2.0 預計要提供的一些新功能。
Citation preview
.
.RESTful Services Using JAX-RS and Jersey蘇國鈞/Monster Su
.
.
RESTful ServicesUsing
JAX-RS and Jersey
http://monstersupreme.blogspot.com
July 29, 2011
.. Profile
國立台灣大學電機工程學研究所畢業現任資訊工業策進會數位教育研究所資訊技術訓練中心教學組長在 Java 領域有十多年的講師教學經驗
熟悉 XML/Web Services、Design Patterns、EJB/JPA 等 Java EE 規格,Struts/Spring Framework/Hibernate 等 Open Source Framework,與JBoss AS、GlassFish 等 Application Server
目前負責雲端運算相關技術的推廣,主要包括 Apache Hadoop、Google App Engine、Microsoft Azure 等 Cloud Platform,與 iOS、Android、Windows Phone 7 等 Smart Handheld 端的整合運用
.. Outline.
. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. SOAP 的開發方式
Amazon Lite:賣 BookBookService - save()/delete()/get()/update()
http://amazonlite.com/bookService?wsdl
賣 DVDDVDService - persist()/remove()/load()/merge()
http://amazonlite.com/dvdService?wsdl
開 InvoiceInvoiceService - insert()/cancel()/locate()/replace()
http://amazonlite.com/invoiceService?wsdl
..Service Calling Made Easy - SOAPhttp://geekandpoke.typepad.com
.. 為什麼不繼續 SOAP 下去...1 SOAP 以前不太好寫 (現在倒是不會)...2 SOAP Overhead 很大 (XML 一直都很煩)...3 SOAP 底層需求很高 (現在還是一樣)...4 SOAP 每個介面都不同 (會的單字都用完了)...5 SOAP 不容易跨平台 (除非不用WS-*)...6 還沒想到...7 …...8 …...9 …...10 懶得想了
.. Take a REST
Amazon Lite:賣 BookHTTP Method - POST/DELETE/GET/PUT
http://amazonlite.com/books賣 DVDHTTP Method - POST/DELETE/GET/PUT
http://amazonlite.com/dvds開 InvoiceHTTP Method - POST/DELETE/GET/PUT
http://amazonlite.com/invoices
..Service Calling Made Easy - RESThttp://geekandpoke.typepad.com
.. 感覺上…
Information 好像是Web 上的一個一個Resource每個 Resource 好像可以用 URI 來表示CRUD 相關操作好像可以透過 Uniform 的方式來進行HTTP 剛好定義了相對應的Method,很適合作為底層 Protocol搭配 HTTP 天生 Stateless 的特性,剛好很有Scalability
.. RESTful Services
全名是 REpresentational State Transfer最早是在 2000 年由 Roy Fielding 提出以 Resource 為核心,不是以 Action 為核心透過 URL 表示Web 上的各種 ResourceResource 可以透過任何方式表示,比方說XML、JSON、HTML、PDF、Raw Data不利用WSDL 定義 Interface直接透過 HTTP 定義的Method 存取資料GET、POST、PUT、與 DELETE以最適當的方式,在 Client 端與 Server 端之間,進行 Resource 狀態的轉移
.. 用 URL/URI 來表示 Resource
REST 否:URL 不一定代表 Resourcehttp://localhost:8080/helloWorld
URL 不只要可以找到 Resourcehttp://localhost:8080/findBookByIsbn?isbn=0596158041
RESTful:URL 真的可以代表唯一的 Resourcehttps://graph.facebook.com/monster.supreme
URL 還是可以有參數https://graph.facebook.com/monster.supreme/picture?type=large
URL 還可以表示想要的 Resource 格式http://api.twitter.com/1/friends/ids.xml?screen_name=MonsterSupreme
.. 一致的 HTTP 介面
.. HTTP Method vs. CRUD
取得 Resource:GET新增 Resource:POST 到既有的 URI實作上通常會傳回 Resource 的 URI修改 Resource:PUT 到既有的 URI刪除 Resource:DELETE取得 Resource 相關資訊:HEAD實作上一般視同沒有 Body 的 GET確認 Resource 支援功能:OPTIONS實作上一般會列出所有支援的Method
.. 類似 WSDL 的介面描述方式
Web Application Description Language:簡稱WADLW3C Member SubmissionSun 的 Marc Hadley (JAX-RS Lead) 代表提出http://www.w3.org/Submission/wadl/為 HTTP-Based Web Application 提供一個Machine Process-able DescriptionSOAP:WSDL=REST:WADL
.. WADL 的長相
1 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>2 <application xmlns="http://research.sun.com/wadl/2006/10">3 <doc xmlns:jersey="http://jersey.dev.java.net/"4 jersey:generatedBy="Jersey: 1.5 01/14/2011 12:36 PM"/>5 <resources base="http://localhost:4848/monitoring/">6 <resource path="reload">7 <method id="reload" name="POST"/>8 </resource>9 <resource path="domain{path:.*}">10 <param xmlns:xs="http://www.w3.org/2001/XMLSchema"11 name="path" style="template" type="xs:string"/>12 <method id="getChildNodes" name="GET">13 <response>14 <representation mediaType="text/html;qs=2}"/>15 <representation mediaType="application/json"/>16 <representation mediaType="application/xml"/>17 </response>18 </method>19 </resource>20 </resources>21 </application>
..REST vs. SOAPRESTful Web Services vs.“Big”Web Serviceshttp://www2008.org/papers/pdf/p805-pautassoA.pdf
REST vs. SOAP 一直是個爭議,不過:RPC 通常強調的是呼叫遠端功能REST 強調的是跟遠端交換資料REST 比 SOAP 容易瞭解,也容易實作REST 不是標準,而是 Architecture Style在一個不太方便撰寫程式的環境下,REST是一種非常好的 Service 提供方式非常適合用在Web 與 Mobile 環境 (雲端)整合Web Resource 時考慮用 REST企業內部比較重視QoS 時考慮用 SOAP
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
..Java 對 RESTful Services 的支援方式J2SE/J2EE 1.4 的 REST 支援
Server 端實作使用 Java Servlet API 或 JSPClient 端實作使用 java.net 套件的 URL 與HttpURLConnection 類別透過 Java API for XML Processing (JAXP) 協助處理 XML 資料透過 Java I/O 的 InputStream 與 OutputStream 類別協助處理 HTTP Request/Response比較直接,但是支援也比較弱
..Java 對 RESTful Services 的支援方式Java SE/EE 5 的 REST 支援
Server 與 Client 端實作都使用 JAX-WS 2.0的 Service 類別,以及 Dispatch 與 Provider 介面透過 Java API for XML Processing (JAXP) 協助處理 XML 資料透過 Java Architecture for XML Binding 2.0(JAXB 2.0) 協助處理 XML 資料與 Java 物件之間的轉換因為是針對 SOAP 設計,所以套用到 REST的時候有點彆扭
..Java 對 RESTful Services 的支援方式Java EE 6 的 REST 支援
JSR 311網址:https://jsr311.dev.java.net/全名是 Java API for RESTful Web Services簡稱 JAX-RS2007 年 2 月宣佈,2008 年 10 月正式定案是 Java EE 6 的一部分套件是 javax.ws.rs.*
以 Annotation 為基礎,至少要 Java SE 5Jersey 是 JAX-RS 的參考實作
..Java 對 RESTful Services 的支援比較Java EE 5 vs. Java EE 6
Java EE 5 (JSR 244):JAX-WS 2.0 (JSR 224)JAXB 2.0 (JSR 222)
Java EE 6 (JSR 316):JAX-WS 2.2 (JSR 224 沒變,實作更新)JAXB 2.2 (JSR 222 沒變,實作更新)JAX-RS 1.1 (JSR 311)
.. JAX-RS 相關實作
目前檯面上的實作:Noelios Technologies 的 RestletJBoss 的 RESTEasyApache 的 CXFSun/Oracle 的 JerseyApache 的 WinkeXo 的 everRESTTrialox ag 的 Triaxrs
..比 JAX-RS 規格還早出現的 Restlethttp://www.restlet.org/
..JBoss 當靠山的 RESTEasyhttp://www.jboss.org/resteasy
..支援 SOAP 與 REST 的 CXFhttp://cxf.apache.org/
..JAX-RS 參考實作 Jerseyhttp://jersey.java.net/
..還在孵蛋中的Winkhttp://incubator.apache.org/wink/
..eXo Platform 的 everRESThttp://trialox.org/archives/323
..支援 OSGi Container 的 Triaxrshttp://code.google.com/p/everrest/
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. 簡化開發過程的 Annotation
URI Path@Path
HTTP Method@POST
@DELETE
@GET
@PUT
@HEAD
@OPTIONS
Injection@Context
Parameter@PathParam
@QueryParam
@HeaderParam
@FormParam
@CookieParam
@MatrixParam
@DefaultValue
Media Handling@Consumes
@Produces
.. 把 Resource 表達成 Resource Class
Lifecycle 預設是 Per-Request,之後就可以 GCJersey 支援 Per-Session 與 @Singleton
1 import javax.ws.rs.GET;2 import javax.ws.rs.Produces;3 import javax.ws.rs.Path;45 @Path("/helloworld")6 public class HelloWorldResource7 {8 @GET9 @Produces("text/plain")10 public String getClichedMessage()11 {12 return "Hello World";13 }14 }
..Resource Class 也有生命週期http://www.devx.com/
.. 用 URI Path Template 標示對應的函式
用 @Path 表示存取路徑沒有常用屬性,內容就是一個相對網址是不是 / 開頭或結尾沒有差別@Path("users/{username: [a-zA-Z][a-zA-Z_0-9]*}")
URI Path Template 可以在 URI 嵌入變數@Path("{bookId}/")
public Book getBook(@PathParam("bookId") int bookId) {...}
URI Path Template 也可以提供參數預設值@Path("/books/")
public List<Book> getBooks(
@DefaultValue("5") @QueryParam("max") int max) {...}
.. 用 Designator 標示 HTTP Method
Request Method Designator:包括 @GET、@POST、@PUT、@DELETE、@HEAD、與 @OPTIONS
沒有常用屬性,用來修飾底下的MethodJAX-RS 實作預設會處理 @HEAD 與 @OPTIONS
1 @Stateless2 @Path("series")3 public class SeriesFacadeREST extends AbstractFacade<Series>4 {5 @DELETE6 @Path("{id}")7 public void remove(@PathParam("id")8 Integer id)9 {10 super.remove(super.find(id));11 }12 }
.. 可以支援的 Resource 格式
@Produces 表示可以產生的輸出格式@Consumes 表示可以接受的輸入格式沒有常用屬性,內容就是MIME 字串或陣列Request Header 宣告 Client 端想要的型式Accept: text/plain;q=0.9, text/html
1 @Stateless2 @Path("series")3 public class SeriesFacadeREST extends AbstractFacade<Series>4 {5 @POST6 @Consumes({"application/xml", "application/json"})7 public void create(Series entity)8 {9 super.create(entity);10 }11 }
.. JAXB/Bean Validation 也支援 JSON
1 @Entity2 @Table(name="Series")3 @XmlRootElement4 @NamedQueries({5 @NamedQuery(name="Series.findAll", query="SELECT s FROM Series s"),6 @NamedQuery(name="Series.findBySeriesId",7 query="SELECT s FROM Series s WHERE s.seriesId = :seriesId")})8 public class Series implements Serializable9 {10 @Id11 @GeneratedValue(strategy=GenerationType.IDENTITY)12 @Basic(optional=false)13 @NotNull14 @Column(name="seriesId")15 private Integer seriesId;1617 @Basic(optional=false)18 @NotNull19 @Size(min=1, max=50)20 @Column(name="seriesName")21 private String seriesName;22 }
.. JPA 的 Entity 是 Resource 的最佳來源
1 @Entity2 @Table(name="Series")3 @XmlRootElement4 public class Series implements Serializable5 {6 @OneToMany(cascade=CascadeType.ALL, mappedBy="series")7 private Collection<Movie> movieCollection;89 @XmlTransient10 public Collection<Movie> getMovieCollection()11 {12 return movieCollection;13 }1415 public void setMovieCollection(Collection<Movie> movieCollection)16 {17 this.movieCollection = movieCollection;18 }19 }
.. Jersey 還提供現成的 Front Controller
1 <?xml version="1.0" encoding="UTF-8"?>2 <web-app version="3.0" ...>3 <servlet>4 <servlet-name>ServletAdaptor</servlet-name>5 <servlet-class>6 com.sun.jersey.spi.container.servlet.ServletContainer7 </servlet-class>8 <load-on-startup>1</load-on-startup>9 </servlet>1011 <servlet-mapping>12 <servlet-name>ServletAdaptor</servlet-name>13 <url-pattern>/resources/*</url-pattern>14 </servlet-mapping>15 </web-app>
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. 各種不同的工具支援
CLI Tool:cURL、WgetGUI Tool:RESTClient、soapUIBrowser Extension
Firefox:Poster、RESTClientChrome:Simple REST Client、REST Console
Web Client:RESTGateIDE Plugin
NetBeans:soapUIEclipse:HTTP4e、JBossTools、soapUI
..Command-Line ToolcURL:http://curl.haxx.se/
..Java ApplicationRESTClient:http://code.google.com/p/rest-client/
..Java ApplicationsoapUI:http://www.soapui.org/
..Firefox AddonRESTClient:https://addons.mozilla.org/en-US/firefox/addon/restclient/
..Chrome ExtensionSimple REST Client:https://chrome.google.com/webstore/
..Chrome ExtensionREST Console:http://www.codeinchaos.com/
..Web ClientRESTGate:http://www.thomas-bayer.com/restgate/
..Eclipse PluginHTTP4e:http://www.ywebb.com/
..Eclipse PluginJBossTools:http://www.jboss.org/tools
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. 沒有定義 Client-Side API
JAX-RS 1.1 是一個 Server-Side Only API,對Client 端並沒有著墨只要 Programming Language 支援 HTTP,就可以拿來寫 RESTful Client有一些 JAX-RS 實作有支援 Client APIJAX-RS 2.0 也有在討論要不要制定 Client API
..URL + HttpURLConnectionJava SE 本身的支援
1 URL url =2 new URL("http://localhost/demo/resources/movies/" + movieId);3 HttpURLConnection conn = (HttpURLConnection) url.openConnection();4 conn.setRequestMethod("GET");5 conn.setRequestProperty("Accept", "application/xml");67 JAXBContext ctx = JAXBContext.newInstance(Movie.class);8 Unmarshaller unmarshaller = ctx.createUnmarshaller();9 StreamSource source = new StreamSource(conn.getInputStream());10 JAXBElement<Movie> element =11 unmarshaller.unmarshal(source, Movie.class);12 Movie movie = element.getValue();1314 conn.disconnect();
..Apache HttpClient 功能比較強大http://hc.apache.org/
1 DefaultHttpClient client = new DefaultHttpClient();2 HttpGet get =3 new HttpGet("http://localhost/demo/resources/movies/" + movieId);4 get.addHeader("Accept", "application/xml");5 HttpResponse response = client.execute(get);67 if (response.getStatusLine().getStatusCode() == 200)8 {9 JAXBContext ctx = JAXBContext.newInstance(Movie.class);10 Unmarshaller unmarshaller = ctx.createUnmarshaller();11 StreamSource source =12 new StreamSource(response.getEntity().getContent());13 JAXBElement<Movie> element =14 unmarshaller.unmarshal(source, Movie.class);15 Movie movie = element.getValue();16 }1718 client.getConnectionManager().shutdown();
..目前可能的解決方案之一Jersey Client API
Pluggable Architecture,目前支援Java SE java.net 套件的 Http(s)URLConnection
Apache 的 HttpClient
(支援 Preemptive Authentication 與 Multipart)
目的是取得現成的 HTTP 實作的優點,但是又做到方便使用WebResource 表示一個封裝了 URI 的 Resource支援 Java 型別byte[]、String、InputStream、File、DataSource、JAXB Bean
支援 JSON,也支援Multipart
.. Jersey 的 Client 叫 NewJerseyClient
1 public class NewJerseyClient {2 private WebResource wr;3 private Client client;4 private String BASE_URI = "http://localhost/demo/resources";56 public NewJerseyClient(String movieId)7 {8 ClientConfig config = new DefaultClientConfig();9 client = Client.create(config);10 String resourcePath =11 MessageFormat.format("movies/{0}", new Object[]{movieId});12 wr = client.resource(BASE_URI).path(resourcePath);13 }1415 public <T> T get_XML(Class<T> responseType) throws Exception {16 return wr.accept(MediaType.APPLICATION_XML).get(responseType);17 }1819 public void delete() throws Exception {20 wr.delete();21 }22 }
.. NewJerseyClient 的 Client
1 NewJerseyClient client = new NewJerseyClient(String.valueOf(movieId));2 String movieString = client.get_XML(String.class);3 client.close();45 JAXBContext ctx = JAXBContext.newInstance(Movie.class);6 Unmarshaller unmarshaller = ctx.createUnmarshaller();7 StringReader reader = new StringReader(movieString);8 StreamSource source = new StreamSource(reader);9 JAXBElement<Movie> element =10 unmarshaller.unmarshal(source, Movie.class);11 Movie movie = element.getValue();
..另一個建議解決方案Spring Framework REST Support
Server-Side:以 Spring MVC 為基礎利用 Annotation 表示 RESTful Resource@Controller/@RequestMapping/@PathVariable
@RequestParam/@RequestHeader/@RequestBody/@ResponseBody...
ContentNegotiatingViewResolver 決定 ViewResolver/View
Client-Side:以 RestTemplate 為 Client 端核心搭配 Jackson JSON Processor (必須是舊版)搭配各種 HttpMessageConverter 來處理資料
.. 超強的 RestTemplate
1 RestTemplate template = new RestTemplate();2 Movie movie = template.getForObject(3 "http://localhost/demos/resources/movies/{movieId}",4 Movie.class, movieId);
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
..資料可以 RESTfulNetBeans Support for RESTful Services
Server-Side:Entity Classes from DatabaseEntity Classes from PatternsRESTful Web Services from Entity ClassesRESTful Web Services from PatternsRESTful Web Services from DatabaseJSF Pages from Entity ClassesTest RESTful Web Services
Client-Side:RESTful Java ClientRESTful JavaScript Client
..NetBeans 提供了一堆 REST WizardEntity Classes from Database
..web.xml 也會自動加入宣告ServletAdapter
..還可以自動產生 JavaScript 測試端Test RESTful Web Services
..當然一定可以產生 Java Client 端RESTful Java Client
.. Server 端講 GAE 嘛會通
不過要做底下的修改:拿掉 @EJB/@Stateless
不支援 EJB拿掉 @PersistenceContext
不支援 DI/CDI拿掉 @Table/@Column/@JoinColumn
不支援 RDBMS拿掉 @NotNull/@Size
不支援 Bean Validation改寫 CriteriaQuery
不支援 JPA 2.0
..API 當然也可以 RESTfulTwitter REST API
..Plurk 也是 RESTfulPlurk API 2.0
..Facebook 當然也要 RESTfulFacebook Graph API
..文字資料可以傳https://graph.facebook.com/monster.supreme
..二進位資料當然也可以傳https://graph.facebook.com/monster.supreme/picture?type=large
..應用程式的管控介面也可以 RESTfulGlassFish Server REST Interfaces
GlassFish Server 3.1 Administration Guidehttp://download.oracle.com/docs/cd/E18930_01/html
/821-2416/gjipx.html
透過 RESTful Interface 進行管理與監控相關功能定義在WADL 檔案之中
http://localhost:4848/management/application.wadl
http://localhost:4848/monitoring/application.wadl
Client 端也有人提供 Java Wrapperhttp://engineeringnotebook.org
/building-a-glassfish-monitoring-client-using-rest
..GlassFish 管控介面的WADLhttp://localhost:4848/monitoring/application.wadl
..透過 Browser 監看 GlassFishhttp://localhost:4848/monitoring/domain
..還可以有不同格式的顯示方式Accept: application/json
..不同系統之間當然可以透過 REST 整合Windows Azure AppFabric Service Bus REST API for Java, PHP
Since the AppFabric Service Bus REST APIcan be used from almost all programminglanguages and operating systems, it makesit very easy for applications written on anyplatform to interoperate with each anotherthrough Windows Azure.You can download the new PHP and Javasamples, as well as all others for all othersupported environments, from CodePlex.
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. Security 怎麼辦Intranet (比較可以確定 User)
可以使用 Java EE 的方式處理透過 @Context 可以取得 SecurityContext
也可以結合 Spring Security 一起使用
Internet (比較無法確定 User)可以使用OAuth 的方式處理
.. 透過 @Context 拿到 SecurityContext
1 @Path("/system")2 @Produces("application/json")3 public class SystemInformationService4 {5 @GET6 @Path("information")7 public Response getSystemInfo(@Context SecurityContext sc)8 {9 System.out.println(sc.getUserPrincipal());10 return Response.ok("foo", MediaType.APPLICATION_JSON).build();11 }12 }
.. Spring Security web.xml 宣告重點
1 <?xml version="1.0" encoding="UTF-8"?>2 <web-app version="3.0" ...>3 <servlet>4 <servlet-name>ServletAdaptor</servlet-name>5 <servlet-class>6 com.sun.jersey.spi.spring.container.servlet.SpringServlet7 </servlet-class>8 <init-param>9 <param-name>10 com.sun.jersey.config.property.packages11 </param-name>12 <param-value>com.monster.service</param-value>13 </init-param>14 </servlet>1516 <servlet-mapping>17 <servlet-name>ServletAdaptor</servlet-name>18 <url-pattern>/resources/*</url-pattern>19 </servlet-mapping>20 </web-app>
..有些 Facebook 的 API 是被管控的Likes - https://graph.facebook.com/me/likes
..Facebook 可以在這邊註冊https://developers.facebook.com/apps/
..取得 Facebook 的 Access TokenAccess Token
..呼叫 Facebook 功能就 OK 了Likes - https://graph.facebook.com/me/likes?access_token=2239|2.AQB|Ha4
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. JAX-RS 2.0/JSR-339三個主角先後離開,所以停頓了一段時間
Marc HadleyPaul SandozRoberto Chinnici
新的 Project LeadSantiago Pericas-GeertsenMarek Potociar
目前主要定調的議題在 Java EE 7 提供更好的 REST 支援Client APIHATEOAS 支援Asynchronous Invocation
..JAX-RS 2.0/JSR-339Client API
希望執行起來能夠又快又順希望有一個 Portable 機制啟用底層特異功能內建提供 Security Mechanism支援 Synchronous 與 Asynchronous 應用Non-Blocking Client 以 Asynchronous HttpClient library for Java 為基礎不會支援 Comet、WebSocket 與 HTTPStreaming (預計是再下一版的功能)
..JAX-RS 2.0/JSR-339Server API
目前只支援 Synchronous 應用預計提供 Asynchronous 應用可能會以 Servlet 3.0 的 Asynchronous 功能實作,也可能採用其他作法
..JAX-RS 2.0/JSR-339Hypermedia as the Engine of Application State
希望提供 HATEOAS 支援http://en.wikipedia.org/wiki/HATEOAS
不只要提供方便的 URI 產生機制還要能夠提供 Link Header 或 Link withinEntity,方便處理 Representation 裡面的Resource也要讓 Client 端能夠輕鬆取得這些 Link,進行後續的處理光靠 Java SE 的 URI 是不夠的JAX-RS 的 UriBuilder/UriTemplate/UriInfo 也不夠方便
..JAX-RS 2.0/JSR-339Model-View-Controller Pattern
JPA 的 Entity 相當於ModelJAX-RS 的 Resource Class 相當於 ControllerJavaServer Pages、FreeMarker、StringTemplate 等技術會被視為 View
..JAX-RS 2.0/JSR-339Bean Validation Integration
提供 Parameter Validation 機制必要時提供有意義的訊息應該會以 Bean Validation API 為基礎
..JAX-RS 2.0/JSR-339CDI Integration
JAX-RS 1.1 定義時間早於 CDI,所以並沒有提供很好的整合JAX-RS 2.0 希望能夠與 CDI 更進一步整合可能因此讓一些 Annotation (@Context) 變成Deprecated
..JAX-RS 2.0/JSR-339Content Negotiation
JAX-RS 1.1 以 Client 端送來的 Accept Header裡面的 q 做決定JAX-RS 2.0 會增加一個由 Server 端決定的 qs
1 <application xmlns="http://research.sun.com/wadl/2006/10">2 <resources base="http://localhost:4848/monitoring/">3 <resource path="domain{path:.*}">4 <param xmlns:xs="http://www.w3.org/2001/XMLSchema"5 name="path" style="template" type="xs:string"/>6 <method id="getChildNodes" name="GET">7 <response>8 <representation mediaType="text/html;qs=2"/>9 <representation mediaType="application/json"/>10 <representation mediaType="application/xml"/>11 </response>12 </method>13 </resource>14 </resources>15 </application>
..JAX-RS 2.0/JSR-339Target Platform
預計內建於 Java EE 7,使用相同的套件名稱javax.ws.rs
但是要求 Java SE 6 與 Java EE 6 就可以實作JAX-RS 2.0
.. .1 Introduction.
. .2 Implementation.
. .3 Server-Side API.
. .4 Development Tool.
. .5 Client-Side API.
. .6 Application.
. .7 Concern.
. .8 Future.
. .9 Summary
.. Summary
RESTful 是趨勢GlassFish 是很好的平台NetBeans 是很棒的開發工具Jersey 是很完整的實作希望可以平順地轉換到 Java EE 7 的 JSR 339
.. References
Pavel Bucek's webloghttp://blogs.oracle.com/PavelBucek/
Japod's bloghttp://blogs.oracle.com/japod/
Jean-Francois Arcand's bloghttp://jfarcand.wordpress.com/
The Aquariumhttp://blogs.oracle.com/theaquarium/
Jersey 1.8 User Guidehttp://jersey.java.net/nonav/documentation/1.8/user-guide.html
..資策會教研所資訊技術訓練中心http://www.iiiedu.org.tw/taipei