Ce didacticiel explique comment créer un client Java RESTful avec RESTEasy client framework , pour exécuter des requêtes « GET » et « POST » vers le service REST créé précédemment. Didacticiel « Jackson + JAX-RS ».
1. Cadre client RESTEasy
Le framework client RESTEasy est inclus dans le module principal de RESTEasy, il vous suffit donc de déclarer «
resteasy-jaxrs.jar
» dans votre fichier
pom.xml
.
Fichier: pom.xml
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.2.1.GA</version> </dependency>
2. Demande GET
Consultez le dernier service REST.
@Path("/json/product") public class JSONService { @GET @Path("/get") @Produces("application/json") public Product getProductInJSON() { Product product = new Product(); product.setName("iPad 3"); product.setQty(999); return product; } //...
Le client RESTEasy pour envoyer une requête «GET».
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import org.apache.http.client.ClientProtocolException; import org.jboss.resteasy.client.ClientRequest; import org.jboss.resteasy.client.ClientResponse; public class RESTEasyClientGet { public static void main(String[]args) { try { ClientRequest request = new ClientRequest( "http://localhost:8080/RESTfulExample/json/product/get"); request.accept("application/json"); ClientResponse<String> response = request.get(String.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } BufferedReader br = new BufferedReader(new InputStreamReader( new ByteArrayInputStream(response.getEntity().getBytes()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
Sortie…
Output from Server .... {"qty":999,"name":"iPad 3"}
3. Requête POST
Consultez également le dernier service REST.
@Path("/json/product") public class JSONService { @POST @Path("/post") @Consumes("application/json") public Response createProductInJSON(Product product) { String result = "Product created : " + product; return Response.status(201).entity(result).build(); } //...
RESTEasy client pour envoyer une demande "POST".
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import org.jboss.resteasy.client.ClientRequest; import org.jboss.resteasy.client.ClientResponse; public class RESTEasyClientPost { public static void main(String[]args) { try { ClientRequest request = new ClientRequest( "http://localhost:8080/RESTfulExample/json/product/post"); request.accept("application/json"); String input = "{\"qty\":100,\"name\":\"iPad 4\"}"; request.body("application/json", input); ClientResponse<String> response = request.post(String.class); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } BufferedReader br = new BufferedReader(new InputStreamReader( new ByteArrayInputStream(response.getEntity().getBytes()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
Sortie…
Output from Server .... Product created : Product[name=iPad 4, qty=100].... === Télécharger le code source Téléchargez-le - lien://wp-content/uploads/2011/07/RESTful-Java-Client-RESTEasyt-Example.zip[RESTful-Java-Client-RESTEytyt-Example.zip](9 Ko) === Références . http://jackson.codehaus.org/%20[Jackson Site Officiel] . lien://services web/jax-rs/restfull-client-java-avec-java-net-url/[RESTful Client Java avec java.net.URL]. lien://services web/jax-rs/reposful-client-java-avec-apache-httpclient/[RESTful Client Java avec Apache HttpClient] lien://tag/client/[client]lien://tag/jax-rs/[jax-rs]lien://tag/resteasy/[resteasy]lien://tag/reposant/[reposant]