Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente |
lang:java:rest [2017/01/09 00:29] – Différence Produces et Consumes root | lang:java:rest [2020/04/27 11:48] (Version actuelle) – maff -> html root |
---|
Dans l'exemple ci-dessous, il faut aussi pour Tomcat et le projet la librairie [[https://github.com/stleary/JSON-java|java-json.jar]] (à mettre dans le dossier ''WebContent/lib'') qui facilitera la réponse au client. | Dans l'exemple ci-dessous, il faut aussi pour Tomcat et le projet la librairie [[https://github.com/stleary/JSON-java|java-json.jar]] (à mettre dans le dossier ''WebContent/lib'') qui facilitera la réponse au client. |
| |
Attention, j'ai eu personnellement beaucoup de mal à faire fonctionner REST sur Eclipse. On pense que tout est bien configurer et ça ne marche pas pendant des heures. Puis d'un coup, on a l'impression que le projet tombe en marche miraculeusement… O_o | Attention, j'ai eu personnellement beaucoup de mal à faire fonctionner REST sur Eclipse. On pense que tout est bien configuré et ça ne marche pas pendant des heures. Puis d'un coup, on a l'impression que le projet tombe en marche miraculeusement… O_o |
| |
J'ai réussi plusieurs fois à faire fonctionner REST sans que ''JAX-RS'' soit activé dans ''Project Facets'' mais jamais avec. Donc prudence. Pareil, avec ''@Path'' mais avec ''@ApplicationPath'' ça ne marche pas (oui, j'ai bien fait hérité de la classe ''Application''). | J'ai réussi plusieurs fois à faire fonctionner REST sans que ''JAX-RS'' soit activé dans ''Project Facets'' mais jamais avec. Donc prudence. Pareil, avec ''@Path'' mais avec ''@ApplicationPath'' ça ne marche pas (oui, j'ai bien fait hérité de la classe ''Application''). |
} | } |
</file> | </file> |
| Utilisation d'un Bean pour architecturer ces données |
| <code java> |
| public class MyBeanParam { |
| @PathParam("p") |
| private String pathParam; |
| |
| @MatrixParam("m") |
| @Encoded |
| @DefaultValue("default") |
| private String matrixParam; |
| |
| @HeaderParam("header") |
| private String headerParam; |
| |
| private String queryParam; |
| |
| public MyBeanParam(@QueryParam("q") String queryParam) { |
| this.queryParam = queryParam; |
| } |
| |
| public String getPathParam() { |
| return pathParam; |
| } |
| } |
| |
| |
| |
| |
| @POST |
| public void post(@BeanParam MyBeanParam beanParam, String entity) { |
| final String pathParam = beanParam.getPathParam(); // contains injected path parameter "p" |
| ... |
| } |
| </code> |
| [[https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/jaxrs-resources.html|JAX-RS Application, Resources and Sub-Resources]] {{ :lang:java:rest:chapter_3._jax-rs_application_resources_and_sub-resources_2020-04-27_11_48_03_am_.html |Archive du 02/03/2020 le 27/04/2020}} |
| |
<file html web.xml> | <file html web.xml> |
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> |
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> | <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
| xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> |
<display-name>REST</display-name> | <display-name>REST</display-name> |
<welcome-file-list> | <welcome-file-list> |