Modifications du comportement de JAX-RS 2.0
L'implémentation de JAX-RS 2.0 inclut des modifications de comportement. These changes might cause applications to behave differently or fail on JAX-RS 2.0 if the applications are upgraded from JAX-RS 1.1.
- In JAX-RS 1.1 and Jersey, if an EJB or CDI class creates a new instance that is returned by the
JAX-RS application.getSingletons() method, the engine uses the returned instance and does not try to
access the instance from the EJB or CDI container. Dans JAX-RS 2.0, dans ce même
scénario, le moteur essaie d'accéder à l'instance depuis le conteneur
EJB ou CDI. If the instance can be accessed, the
retrieved instance is used. But if the instance cannot be accessed, the returned instance from the
getSingletons() method is used. Exemple :
@Override public SetObject getSingletons() { SetObject objs = new HashSetObject(); objs.add(new CDIInjectResource()); objs.add(new EJBInjectResource()); return objs; }
- JAX-RS 2.0 inclut de nombreuses modifications d'API lorsqu'il traite le fichier à plusieurs parties. For example, in JAX-RS 1.1, the @FormParam can be used to handle the MultiPart file, but in JAX-RS 2.0, only @IMultipartBody or @IAttachment can be used to handle the MultiPart file. For more information, see Configuring a resource to receive multipart/form-data parts from an HTML form submission in JAX-RS 2.0.
- The jackson packages that are displayed as a third-party API in JAX-RS 1.1 are no longer displayed in JAX-RS 2.0. Si vous voulez utiliser des API org.codehaus.jackson dans votre application, vous devez compresser les packages jackson dans votre application.
- If you specify javax.ws.rs.core.Application for the servlet name in the
web.xml file, the getClasses method in the Application object,
which is injected by @Context, does not return the resource
classes.
<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>
- D'après la spécification JAX-RS 2.0, un fournisseur
est
une classe qui implémente une ou plusieurs interfaces JAX-RS et qui
peut être annotée avec @Provider pour la
reconnaissance automatique.
In the scenario, a class has @Local annotation that refers to a provider interface,
but it does not implement any POJO provider interface, and then it is an invalid provider. Exemple :
@Stateless @Local(OneLocalInterfaceMyOtherStuffMessageBodyWriter.class) public class OneLocalInterfaceMyOtherStuffProvide
- Si vous utilisez les annotations @Consumes et
@Produces de MessageBodyReader
et MessageBodyWriter, certains types de support pris en
charge peuvent être restreints. Use the
isReadable method or isWriteable method to check the media type.
Exemple :
@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; } ... }
- Vous pouvez utiliser le traitement asynchrone dans JAX-RS 2.0 pour traiter les unités d'exécution. Pour plus d'informations, voir la rubrique Traitement asynchrone.
- None of the Wink APIs that are displayed as third-party APIs in JAX-RS 1.1 are supported in
JAX-RS 2.0. Voici une
liste partielle :
- org.apache.wink.common.model.atom.AtomEntry. For more information about integrating JAX-RS 2.0 with Atom, see Intégration de JAX-RS 2.0 à Atom.
- org.apache.wink.client.handlers.BasicAuthSecurityHandler. Si vous voulez utiliser l'authentification de base dans JAX-RS 2.0,
consulter les fragments de code suivants :
- Use ClientRequestFilter through the JAX-RS 2.0 standard Client API as shown in
the code example:
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); } }
- Enregistrez dans
ClientBuilder :
ClientBuilder cb = ClientBuilder.newBuilder(); cb.register(new BasicAuthFilter("user","password"));
- Use ClientRequestFilter through the JAX-RS 2.0 standard Client API as shown in
the code example:
- org.apache.wink.client.handlers.LtpaAuthSecurityHandler. If you want to use the LTPA-based security client to secure downstream resources, see Sécurisation des ressources JAX-RS en aval.
- org.apache.wink.server.internal.providers.exception.EJBAccessExceptionMapper. Cette API n'est plus prise en charge car il s'agit d'une exception spécifiée par Wink. Vous pouvez définir votre élément ExceptionMapper pour mapper l'élément EJBAccessException.
- com.ibm.websphere.jaxrs.server.IBMRestFilter. Cette API n'est plus prise en charge car elle est basée sur un filtre Wink.
Remarque : Detect if there are wink jar packages in your application. If there are any wink packages in your application, you must do the following steps:- Assurez-vous qu'une sous-classe Application est définie.
- At least one of getClasses and getSingletons must not return null.
- Pour plus d'informations sur les propriétés client prises en charge qui peuvent être utilisées dans le client JAX-RS 2.0, voir Configuration du client JAX-RS 2.0.
- If you want to use the Secure Sockets Layer (SSL) function in JAX-RS 2.0, do the
following steps:
- Configurez les propriétés SSL dans la console d'administration de WebSphere Application Server Traditional.
Pour plus d'informations, voir la rubrique Création d'une configuration SSL (Secure Sockets Layer).
- Activez la sécurité pour votre application JAX-RS et configurez cette dernière de sorte qu'elle utilise un canal SSL pour le transport lorsqu'elle appelle les ressources REST.
Lors du développement ou du déploiement d'applications, éditez le fichier web.xml afin d'y ajouter une contrainte de sécurité indiquant que SSL doit être utilisé pour vos ressources. Pour plus de détails sur l'activation de SSL pour votre application, consultez la rubrique relative à la sécurisation des applications JAX-RS dans le conteneur Web.
L'élément suivant, à l'intérieur de l'élément security-constraint, définit l'imposition de SSL pour l'application :<user-data-constraint id="UserDataConstraint_1"> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint>
- Pour activer la couche SSL client lorsque vous développez votre application client, ajoutez une propriété client dans votre code d'application client.
Définissez la clé de propriété client com.ibm.ws.jaxrs.client.ssl.config et attribuez-lui la valeur true. Voir le fragment de code suivant :
ClientBuilder cb = ClientBuilder.newBuilder(); cb.property("com.ibm.ws.jaxrs.client.ssl.config", "NodeDefaultSSLSettings");
Conseil : La valeur de propriété correspond à l'alias SSL de serveur que vous définissez. For more information, go to Application servers->server n, where n is the number that you assigned to the application server.->Web container transport chains->WCInboundDefaultSecure->SSL inbound channel (SSL_2) to check it under the SSL configuration field. - Déployez votre application client sur WebSphere Application Server Traditional à l'aide de la console d'administration.
- Démarrez l'application client dans la console d'administration de WebSphere Application Server Traditional.
Pour démarrer votre application, accédez à Applications->Types d'application->Applications d'entreprise WebSphere->Démarrer.
- Configurez les propriétés SSL dans la console d'administration de WebSphere Application Server Traditional.