Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| slam4:gui:rest [2015/03/15 19:33] – [2- JSON] jcheron | slam4:gui:rest [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 78: | Ligne 78: | ||
| </ | </ | ||
| - | ==== -- De JSON au model ==== | + | === -- De JSON au model === |
| - | Créer la méthode suivante : | + | Créer la méthode suivante |
| <sxh java; | <sxh java; | ||
| Ligne 88: | Ligne 88: | ||
| } | } | ||
| ... | ... | ||
| + | </ | ||
| + | |||
| + | Ajouter la méthode Main dans la classe pour la tester : | ||
| + | |||
| + | <sxh java; | ||
| + | ... | ||
| + | public static void main(String args[]){ | ||
| + | TestJSON jsonTest=new TestJSON(); | ||
| + | String jsonStr=" | ||
| + | Model m=jsonTest.jsonToModel(jsonStr); | ||
| + | System.out.println(m); | ||
| + | |||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | L' | ||
| + | |||
| + | < | ||
| + | |||
| + | === -- Du model à JSON === | ||
| + | |||
| + | Ajouter la méthode suivante retournant une chaîne JSON construite à partir d'une instance de la classe **Model** : | ||
| + | |||
| + | <sxh java; | ||
| + | ... | ||
| + | public String modelToJson(Model m){ | ||
| + | return gson.toJson(m).toString(); | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Ajouter dans la méthode Main le code suivant : | ||
| + | |||
| + | <sxh java; | ||
| + | ... | ||
| + | public static void main(String args[]){ | ||
| + | TestJSON jsonTest=new TestJSON(); | ||
| + | String jsonStr=" | ||
| + | Model m=jsonTest.jsonToModel(jsonStr); | ||
| + | System.out.println(m); | ||
| + | m.setName(" | ||
| + | System.out.println(jsonTest.modelToJson(m)); | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | L' | ||
| + | |||
| + | < | ||
| + | {" | ||
| + | |||
| + | ===== -- Http requests ===== | ||
| + | |||
| + | Pour mettre en oeuvre les tests, vous devez disposer d'un serveur HTTP hébergeant un service Rest. | ||
| + | ==== -- GET ==== | ||
| + | |||
| + | Créer une classe TestHttp, instanciant un objet Gson qui nous servira pour les conversions JSON< | ||
| + | |||
| + | <sxh java; | ||
| + | public class TestHttp { | ||
| + | private Gson gson; | ||
| + | |||
| + | public TestHttp() { | ||
| + | gson = new GsonBuilder() | ||
| + | .setDateFormat(" | ||
| + | .create(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | Implémmenter la méthode getHttp : | ||
| + | <sxh java; | ||
| + | ... | ||
| + | public String getHTML(String urlToRead) throws ClientProtocolException, | ||
| + | String result=""; | ||
| + | CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
| + | try { | ||
| + | HttpGet getRequest = new HttpGet(urlToRead); | ||
| + | ResponseHandler< | ||
| + | result = httpClient.execute(getRequest, | ||
| + | }finally { | ||
| + | httpClient.close();; | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Ajouter la méthode **main** dans la classe pour tester le Get, n' | ||
| + | |||
| + | <sxh java; | ||
| + | ... | ||
| + | public static void main(String args[]) { | ||
| + | TestHttp test = new TestHttp(); | ||
| + | |||
| + | try { | ||
| + | String result = test.getHTML(" | ||
| + | System.out.println(result); | ||
| + | |||
| + | } catch (IOException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | ==== -- POST ==== | ||
| + | |||
| + | Implémmenter la méthode restPostJSON : | ||
| + | <sxh java; | ||
| + | ... | ||
| + | public String restPostJSON(String urlToRead, Object o) throws ClientProtocolException, | ||
| + | String result = ""; | ||
| + | CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
| + | try { | ||
| + | HttpPost postRequest = new HttpPost(urlToRead); | ||
| + | postRequest.setHeader(" | ||
| + | postRequest.setHeader(" | ||
| + | String jsonString = gson.toJson(o); | ||
| + | StringEntity params = new StringEntity(jsonString); | ||
| + | params.setContentType(" | ||
| + | params.setContentEncoding(" | ||
| + | postRequest.setEntity(params); | ||
| + | ResponseHandler< | ||
| + | result = httpClient.execute(postRequest, | ||
| + | } finally { | ||
| + | httpClient.close(); | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Modifier la méthode **main** de la classe pour tester le restPostJSON : | ||
| + | |||
| + | <sxh java; | ||
| + | ... | ||
| + | public static void main(String args[]) { | ||
| + | TestHttp test = new TestHttp(); | ||
| + | |||
| + | try { | ||
| + | ... | ||
| + | System.out.println(test.restPostJSON( | ||
| + | " | ||
| + | |||
| + | } catch (IOException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== -- POST Classique ===== | ||
| + | |||
| + | Le post classique est légèrement plus complexe, puisqu' | ||
| + | * La conversion en JsonObject de l' | ||
| + | * L' | ||
| + | * La définition du content-type de la requête : **" | ||
| + | |||
| + | |||
| + | Implémmenter la méthode postJSON : | ||
| + | <sxh java; | ||
| + | ... | ||
| + | public String postJSON(String urlToRead, Object o) | ||
| + | throws ClientProtocolException, | ||
| + | String result = ""; | ||
| + | CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
| + | try { | ||
| + | HttpPost postRequest = new HttpPost(urlToRead); | ||
| + | postRequest.setHeader(" | ||
| + | List< | ||
| + | |||
| + | JsonElement elm = gson.toJsonTree(o); | ||
| + | JsonObject jsonObj = elm.getAsJsonObject(); | ||
| + | for (Map.Entry< | ||
| + | nameValuePairs.add(new BasicNameValuePair(entry.getKey(), | ||
| + | .getValue().getAsString())); | ||
| + | } | ||
| + | postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs)); | ||
| + | ResponseHandler< | ||
| + | result = httpClient.execute(postRequest, | ||
| + | } finally { | ||
| + | httpClient.close(); | ||
| + | ; | ||
| + | } | ||
| + | return result; | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | Modifier la méthode **main** de la classe pour tester le POST classique, il s'agit ici d'un exemple avec une classe User : | ||
| + | |||
| + | <sxh java; | ||
| + | ... | ||
| + | public static void main(String args[]) { | ||
| + | TestHttp test = new TestHttp(); | ||
| + | |||
| + | try { | ||
| + | String result = test.getHTML(" | ||
| + | System.out.println(result); | ||
| + | |||
| + | System.out.println(test.postJSON( | ||
| + | " | ||
| + | " | ||
| + | |||
| + | } catch (IOException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | ===== -- Session ===== | ||
| + | |||
| + | Pour conserver la session, on instancie un HttpContext, | ||
| + | |||
| + | |||
| + | <sxh java> | ||
| + | |||
| + | private HttpContext httpContext; | ||
| + | private CloseableHttpClient httpClient; | ||
| + | private CookieStore cookieStore; | ||
| + | |||
| + | protected void createCookieStore() { | ||
| + | httpClient = HttpClients.createDefault(); | ||
| + | cookieStore = new BasicCookieStore(); | ||
| + | httpContext = new BasicHttpContext(); | ||
| + | httpContext.setAttribute(HttpClientContext.COOKIE_STORE, | ||
| + | } | ||
| + | |||
| + | public TestHttp() { | ||
| + | gson = new GsonBuilder().setDateFormat(" | ||
| + | createCookieStore(); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Utilisation et passage du HttpContext : | ||
| + | <sxh java> | ||
| + | result = httpClient.execute(getRequest, | ||
| + | ... | ||
| + | result = httpClient.execute(postRequest, | ||
| + | |||
| </ | </ | ||