
![[8.5.5.6 或更新版本]](../ng_v8556.gif)
JAX-RS 2.0 行為變更
JAX-RS 2.0 實作有了一些行為變更,可能使得從 JAX-RS 1.1 移轉的應用程式,在 JAX-RS 2.0 中採取不同的行為或者執行失敗。
下列清單說明 JAX-RS 1.1 與 JAX-RS 2.0 之間的差異:
- 在 JAX-RS 1.1 與 Jersey 中,如果 EJB 或 CDI 類別建立了新實例,且這個實例是由 JAX-RS application.getSingletons() 方法傳回,則傳回的實例是供引擎使用,且引擎不會嘗試從 EJB 或 CDI 儲存器存取實例。在 JAX-RS 2.0 中,同樣情況下引擎會嘗試從 EJB 或 CDI 儲存器存取實例。如果實例可供存取,則會使用所擷取的實例。如果實例無法存取,則會使用 getSingletons() 方法傳回的實例。例如:
@Override public SetObject getSingletons() { SetObject objs = new HashSetObject(); objs.add(new CDIInjectResource()); objs.add(new EJBInjectResource()); return objs; }
- jackson 套件以協力廠商 API 形式顯現於 JAX-RS 1.1 中,但在 JAX-RS 2.0 中已不再顯現。如果您想在應用程式中使用任何 org.codehaus.jackson API,必須在您的應用程式中壓縮 jackson 套件。
- 如果您在 web.xml 檔中,指定 javax.ws.rs.core.Application 作為 Servlet 名稱,應用程式物件中 @Context 所注入的 getClasses 方法將不會傳回資源類別。
<servlet> <servlet-name>javax.ws.rs.core.Application</servlet-name> </servlet> <servlet-mapping> <servlet-name>javax.ws.rs.core.Application*</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
- JAX-RS 2.0 規格指出,提供者是一個會實作一或多個 JAX-RS 介面並可標註 @Provider 以便自動探索的類別。假設在此實務中類別有 @Local 註釋,而會參照提供者介面,但不會實作任何 POJO 提供者介面,則它是無效的提供者。例如:
@Stateless @Local(OneLocalInterfaceMyOtherStuffMessageBodyWriter.class) public class OneLocalInterfaceMyOtherStuffProvide
- 如果您使用 MessageBodyReader 和 MessageBodyWriter @Consumes 和 @Produces 註釋,某些支援的媒體類型可能受到限制。請使用
isReadable 或 isWriteable 方法,來檢查媒體類型。例如:
@Provider @Consumes("custom/type") @Produces("custom/type") @Singleton public class MyMessageBodyReaderAndWriter implements MessageBodyReader,MessageBodyWriter { public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { if (mediaType.toString().equals("custom/type")) return true; return false; } public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { if (mediaType.toString().equals("custom/type")) return true; return false; } ... }
- 您可以使用 JAX-RS 2.0 中的非同步處理,來處理執行緒。如需相關資訊,請參閱非同步處理。
- 不支援顯現成協力廠商 API 的 Wink API。下列清單說明不支援的 Wink API(但不只這些):
- org.apache.wink.common.model.atom.AtomEntry。在 JAX-RS 2.0 中,API 倚賴 Wink 執行時期。
- org.apache.wink.client.handlers.BasicAuthSecurityHandler。如果您想在 JAX-RS 2.0 中使用基本鑑別 (BA),請參閱下列程式碼 Snippet:
- 透過 JAX-RS 2.0 標準用戶端 API 來使用 ClientRequestFilter,如程式碼範例所示:
import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.ws.rs.client.ClientRequestContext; import javax.ws.rs.client.ClientRequestFilter; import javax.ws.rs.core.MultivaluedMap; import javax.xml.bind.DatatypeConverter; public class BasicAuthFilter implements ClientRequestFilter { private final String usr; private final String pwd; public BasicAuthFilter(String usr, String pwd) { this.usr = user; this.pwd = pwd; } public void filter(ClientRequestContext requestContext) throws IOException { MultivaluedMap<String, Object> headers = requestContext.getHeaders(); String token = this.usr + ":" + this.pwd; final String basicAuthentication ="Basic " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8")); headers.add("Authorization", basicAuthentication); } }
- 向 ClientBuilder 登錄:
ClientBuilder cb = ClientBuilder.newBuilder(); cb.register(new BasicAuthFilter("user","password"));
- 透過 JAX-RS 2.0 標準用戶端 API 來使用 ClientRequestFilter,如程式碼範例所示:
- org.apache.wink.client.handlers.LtpaAuthSecurityHandler。如果您想使用 LTPA 型安全用戶端,來維護下游資源的安全,請參閱維護下游 JAX-RS 資源的安全。
- org.apache.wink.server.internal.providers.exception.EJBAccessExceptionMapper。不再支援此 API,因為它是 Wink 指定的 ExceptionMapper。您可以定義自己的 ExceptionMapper 來對映 EJBAccessException。
- com.ibm.websphere.jaxrs.server.IBMRestFilter。不再支援此 API,因為它是以 Wink Filter 為基礎。
- 如需可在 JAX-RS 2.0 用戶端中使用的支援用戶端內容的相關資訊,請參閱配置 JAX-RS 2.0 用戶端。
- 如果您想在 JAX-RS 2.0 中使用 Secure Sockets Layer (SSL) 功能,請執行下列步驟:
- 啟用 ssl-1.0 或 appSecurity-2.0 特性。若要使用 LTPA 記號功能,則需要 appSecurity-2.0 特性。註: ssl-1.0 特性是 appSecurity-2.0 特性的子特性。如果您啟用 jaxrsClient-2.0 和 ssl-1.0 特性,會自動啟用 appSecurity-2.0 特性。
- 在 JAX-RS 2.0 用戶端程式碼中啟用 com.ibm.ws.jaxrs.client.ssl.config 內容,如下所示:
ClientBuilder cb = ClientBuilder.newBuilder(); Client c = cb.build(); c.property("com.ibm.ws.jaxrs.client.ssl.config", "mySSLConfig"); //mySSLConfig is the ssl ref id in Liberty server.xml
註: 這個內容可將 Liberty SSL 配置連結至 ClientBuilder、Client 和 WebTarget 範圍。
- 啟用 ssl-1.0 或 appSecurity-2.0 特性。若要使用 LTPA 記號功能,則需要 appSecurity-2.0 特性。
- 如果您想在 JAX-RS 2.0 伺服器執行時期中使用 Wink Client,請執行下列步驟:
- 從 http://wink.apache.org/downloads.html 下載 Apache Wink 和相關的 JAR 檔。
- 從 http://hc.apache.org/ 下載 Apache HTTP 和相關的 JAR 檔。
註: 如果未啟用 JAX-RS 2.0 特性,您也必須下載 JAX-RS API,並新增至 third-party lib。從 https://jax-rs-spec.java.net/nonav/ 下載 API。- 將所有 JAR 檔儲存到 <third-party lib> 目錄。
- 將 <third-party lib> 位置新增至 server.xml 檔:
<library id="thirdPartyLib"> <fileset dir=" <third-party lib>" includes="*.jar" scanInterval="5s"/> </library> <enterpriseApplication id="<Your Ear ID>" location="<Your Ear Name>" name="<Your Ear Name>"> <classloader commonLibraryRef="thirdPartyLib"/> </enterpriseApplication>
註: 如需用戶端與伺服器 API 中的非同步處理相關資訊,請參閱 JSR 339:
JAX-RS 2.0: The Java API for RESTful Web Services 的第 8 章 "Specification"。