Outils pour utilisateurs

Outils du site


lang:java:rest

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
lang:java:rest [2017/01/08 23:34] – Création rootlang:java:rest [2020/04/27 11:48] (Version actuelle) – maff -> html root
Ligne 5: Ligne 5:
 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 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'').
  
 <file java Serveur.java> <file java Serveur.java>
 package com.llgc; package com.llgc;
  
-// import javax.ws.rs.ApplicationPath;+import java.io.File; 
 +import java.io.FileOutputStream; 
 +import java.io.IOException; 
 +import java.io.InputStream; 
 +import java.io.OutputStream; 
 import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
 import javax.ws.rs.GET; import javax.ws.rs.GET;
 +import javax.ws.rs.POST;
 import javax.ws.rs.Path; import javax.ws.rs.Path;
 import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces; import javax.ws.rs.Produces;
-// import javax.ws.rs.core.Application; 
-import javax.ws.rs.core.MediaType; 
  
 +import javax.ws.rs.core.MediaType;
 +import javax.ws.rs.core.Response;
 +import javax.ws.rs.core.Response.ResponseBuilder;
 +/*
 +import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
 +import org.glassfish.jersey.media.multipart.FormDataParam;
 +*/
 import org.json.JSONException; import org.json.JSONException;
 import org.json.JSONObject; import org.json.JSONObject;
  
-@/*Application*/Path("/euro"+@Path("/euro"
-public class Serveur/* extends Application*/ {+public class Serveur 
 +  // Ici, le path est cumulatif avec le "/euro"
 +  // On fait donc bien référence au chemin "/euro/{f}" avec {f} : un nombre 
 +  // flottant
   @Path("{f}")   @Path("{f}")
 +  // Méthode GET
   @GET   @GET
 +  // Mime Type en sortie
   @Produces(MediaType.APPLICATION_XML)   @Produces(MediaType.APPLICATION_XML)
 +  // Mime Type en entrée
   @Consumes(MediaType.APPLICATION_XML)   @Consumes(MediaType.APPLICATION_XML)
 +  // On dit que le f est extrait du path et que c'est un double.
   public String traitementXml(@PathParam("f") Double f) {   public String traitementXml(@PathParam("f") Double f) {
     return "<conversion><euros>" + f / 6.65 + "</euros><francs>" + f + "</francs></conversion>";     return "<conversion><euros>" + f / 6.65 + "</euros><francs>" + f + "</francs></conversion>";
Ligne 51: Ligne 72:
     jsonObject.put("Euris", f / 6.65);     jsonObject.put("Euris", f / 6.65);
     return jsonObject.toString();     return jsonObject.toString();
 +  }
 +
 +  @GET
 +  @Path("/down")
 +  @Produces("image/png")
 +  // Le client veut télécharger
 +  public Response download() {
 +    File file = new File("image.png");
 +    ResponseBuilder response = Response.ok((Object) file);
 +    response.header("Content-Disposition", "attachment; filename=test.png");
 +    return response.build();
 +  }
 +
 +  @Path("/upload")
 +  @POST
 +  @Consumes(MediaType.MULTIPART_FORM_DATA)
 +  // Le client veut uploader
 +  public Response upload(@FormDataParam("file") InputStream uploadedInputStream,
 +      @FormDataParam("file") FormDataContentDisposition fileDetail) {
 +    String uploadedFileLocation = "./" + "Jersey_" + fileDetail.getFileName();
 +    saveToFile(uploadedInputStream, uploadedFileLocation);
 +    String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
 +    return Response.status(200).entity(output).build();
 +  }
 +
 +  private void saveToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
 +    try {
 +      OutputStream out = null;
 +      int read = 0;
 +      byte[] bytes = new byte[1024];
 +      out = new FileOutputStream(new File(uploadedFileLocation));
 +      while ((read = uploadedInputStream.read(bytes)) != -1) {
 +        out.write(bytes, 0, read);
 +      }
 +      out.flush();
 +      out.close();
 +    } catch (IOException e) {
 +      e.printStackTrace();
 +    }
   }   }
 } }
 </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>
Ligne 83: Ligne 179:
 </web-app> </web-app>
 </file> </file>
 +
 +====Exemple de client====
 +<code java>
 +import java.net.URI;
 +
 +import javax.ws.rs.client.Client;
 +import javax.ws.rs.client.ClientBuilder;
 +import javax.ws.rs.client.WebTarget;
 +import javax.ws.rs.core.MediaType;
 +import javax.ws.rs.core.Response;
 +
 +public class ClientJersey {
 +  public static void delete() {
 +    String url = "http://localhost:8080/client/del/0";
 +    URI uri = URI.create(url);
 +
 +    final Client client = ClientBuilder.newClient();
 +    WebTarget webTarget = client.target(uri);
 +
 +    Response response = webTarget.request().delete();
 +
 +    if (response.getStatus() != 200) {
 +      throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
 +    }
 +
 +    System.out.println("Output delete from Server .... \n" + response.getStatus());
 +  }
 +
 +  public static void main(String[] args) {
 +    try {
 +      delete();
 +
 +      String url = "http://localhost:8080/helloworld/xml";
 +      URI uri = URI.create(url);
 +
 +      final Client client = ClientBuilder.newClient();
 +      WebTarget webTarget = client.target(uri);
 +
 +      Response response = webTarget.request(MediaType.APPLICATION_XML).get();
 +
 +      if (response.getStatus() != 200) {
 +        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
 +      }
 +
 +      String output = response.readEntity(String.class);
 +
 +      System.out.println("Output XML from Server .... \n");
 +      System.out.println(output);
 +    } catch (Exception e) {
 +      e.printStackTrace();
 +    }
 +  }
 +}
 +</code>
  
 =====Erreurs===== =====Erreurs=====
Ligne 92: Ligne 242:
 </code> </code>
  
-===Jersey 2=== 
-<code xml> 
-<servlet> 
-  <servlet-name>Jersey Web Application</servlet-name> 
-  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
-  <init-param> 
-    <param-name>jersey.config.server.provider.packages</param-name> 
-    <param-value>com.llgc</param-value> 
-  </init-param> 
-  <load-on-startup>1</load-on-startup> 
-</servlet> 
-</code> 
- 
- 
- 
- 
- 
-La [[ide:eclipse:tomcat#modifier_la_variable_systeme_classpath|Classpath]] de Tomcat ne possède pas les libraires des dossiers ''lib'' et ''ext'' de Jersey. 
lang/java/rest.1483914847.txt.gz · Dernière modification : 2017/01/08 23:34 de root