lang:java:rest
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
lang:java:rest [2017/01/08 23:34] – Création root | lang:java:rest [2020/04/27 11:48] (Version actuelle) – maff -> html root | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
Dans l' | Dans l' | ||
+ | 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' | ||
+ | |||
+ | J'ai réussi plusieurs fois à faire fonctionner REST sans que '' | ||
<file java Serveur.java> | <file java Serveur.java> | ||
package com.llgc; | package com.llgc; | ||
- | // import | + | import |
+ | 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; | ||
- | @/ | + | @Path("/ |
- | public class Serveur/* extends Application*/ { | + | public class Serveur |
+ | | ||
+ | // On fait donc bien référence au chemin "/euro/{f}" avec {f} : un nombre | ||
+ | // flottant | ||
@Path(" | @Path(" | ||
+ | // 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(" | public String traitementXml(@PathParam(" | ||
return "< | return "< | ||
Ligne 51: | Ligne 72: | ||
jsonObject.put(" | jsonObject.put(" | ||
return jsonObject.toString(); | return jsonObject.toString(); | ||
+ | } | ||
+ | |||
+ | @GET | ||
+ | @Path("/ | ||
+ | @Produces(" | ||
+ | // Le client veut télécharger | ||
+ | public Response download() { | ||
+ | File file = new File(" | ||
+ | ResponseBuilder response = Response.ok((Object) file); | ||
+ | response.header(" | ||
+ | return response.build(); | ||
+ | } | ||
+ | |||
+ | @Path("/ | ||
+ | @POST | ||
+ | @Consumes(MediaType.MULTIPART_FORM_DATA) | ||
+ | // Le client veut uploader | ||
+ | public Response upload(@FormDataParam(" | ||
+ | @FormDataParam(" | ||
+ | String uploadedFileLocation = " | ||
+ | saveToFile(uploadedInputStream, | ||
+ | String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation; | ||
+ | return Response.status(200).entity(output).build(); | ||
+ | } | ||
+ | |||
+ | private void saveToFile(InputStream uploadedInputStream, | ||
+ | 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, | ||
+ | } | ||
+ | out.flush(); | ||
+ | out.close(); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
} | } | ||
} | } | ||
</ | </ | ||
+ | Utilisation d'un Bean pour architecturer ces données | ||
+ | <code java> | ||
+ | public class MyBeanParam { | ||
+ | @PathParam(" | ||
+ | private String pathParam; | ||
+ | |||
+ | @MatrixParam(" | ||
+ | @Encoded | ||
+ | @DefaultValue(" | ||
+ | private String matrixParam; | ||
+ | |||
+ | @HeaderParam(" | ||
+ | private String headerParam; | ||
+ | |||
+ | private String queryParam; | ||
+ | |||
+ | public MyBeanParam(@QueryParam(" | ||
+ | this.queryParam = queryParam; | ||
+ | } | ||
+ | |||
+ | public String getPathParam() { | ||
+ | return pathParam; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | @POST | ||
+ | public void post(@BeanParam MyBeanParam beanParam, String entity) { | ||
+ | final String pathParam = beanParam.getPathParam(); | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | [[https:// | ||
<file html web.xml> | <file html web.xml> | ||
<?xml version=" | <?xml version=" | ||
- | <web-app xmlns: | + | <web-app xmlns: |
+ | | ||
< | < | ||
< | < | ||
Ligne 83: | Ligne 179: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | ====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 = " | ||
+ | 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(" | ||
+ | } | ||
+ | |||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | try { | ||
+ | delete(); | ||
+ | |||
+ | String url = " | ||
+ | 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(" | ||
+ | } | ||
+ | |||
+ | String output = response.readEntity(String.class); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(output); | ||
+ | } catch (Exception e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
=====Erreurs===== | =====Erreurs===== | ||
Ligne 92: | Ligne 242: | ||
</ | </ | ||
- | ===Jersey 2=== | ||
- | <code xml> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | < | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | La [[ide: |
lang/java/rest.1483914847.txt.gz · Dernière modification : 2017/01/08 23:34 de root