Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. package com.vladimirbykov08.rest_client_crud;
  2.  
  3. import com.sun.istack.NotNull;
  4. import com.sun.jersey.api.client.Client;
  5. import com.sun.jersey.api.client.ClientResponse;
  6. import com.sun.jersey.api.client.GenericType;
  7. import com.sun.jersey.api.client.WebResource;
  8.  
  9. import javax.ws.rs.core.MediaType;
  10. import java.util.List;
  11.  
  12.  
  13. public class App {
  14.     private static final String URL = "http://localhost:8585/rest/persons";
  15.  
  16.     public static void main(String[] args) {
  17.         Client client = Client.create();
  18.         //printList(getPersons(client, null, null, null, null, null));
  19.         //System.out.println();
  20.         //printList(getPersons(client, "V2","BY" , null, null, null));
  21.         String id = create(client,"Vladimir","Bykov",24,"VB", "QA");
  22.         System.out.println(id);
  23.         update(client,Integer.parseInt(id),"1",null,null,null,null);
  24.        // printList(getPersons(client, null, null, null, null, null));
  25.         //delete(client,1);
  26.     }
  27.  
  28.     private static List<Person> getPersons(Client client,
  29.                                            String name,
  30.                                            String surname,
  31.                                            Integer age,
  32.                                            String abbreviation,
  33.                                            String job) {
  34.         WebResource webResource = client.resource(URL);
  35.         if (name != null) {
  36.             webResource = webResource.queryParam("name", name);
  37.         }
  38.         if (surname != null) {
  39.             webResource = webResource.queryParam("surname", surname);
  40.         }
  41.         if (age != null) {
  42.             webResource = webResource.queryParam("age", age.toString());
  43.         }
  44.         if (abbreviation != null) {
  45.             webResource = webResource.queryParam("abbreviation", abbreviation);
  46.         }
  47.         if (job != null) {
  48.             webResource = webResource.queryParam("job", job.toString());
  49.         }
  50.         ClientResponse response =
  51.                 webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
  52.         if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
  53.             throw new IllegalStateException("Request failed");
  54.         }
  55.         GenericType<List<Person>> type = new GenericType<List<Person>>() {
  56.         };
  57.         return response.getEntity(type);
  58.     }
  59.     private static String update(Client client,
  60.                                        @NotNull Integer id,
  61.                                        String name,
  62.                                        String surname,
  63.                                        Integer age,
  64.                                        String abbreviation,
  65.                                        String job) {
  66.         WebResource webResource = client.resource(URL);
  67.         if (name != null) {
  68.             webResource = webResource.queryParam("name", name);
  69.         }
  70.         if (surname != null) {
  71.             webResource = webResource.queryParam("surname", surname);
  72.         }
  73.         if (age != null) {
  74.             webResource = webResource.queryParam("age", age.toString());
  75.         }
  76.         if (abbreviation != null) {
  77.             webResource = webResource.queryParam("abbreviation", abbreviation);
  78.         }
  79.         if (age != null) {
  80.             webResource = webResource.queryParam("job",job);
  81.         }
  82.         if (id != null)
  83.             webResource = webResource.queryParam("id", id.toString());
  84.         ClientResponse response =
  85.                 webResource.accept(MediaType.APPLICATION_JSON)
  86.                         .post(ClientResponse.class);
  87.         System.out.println(response.getStatus());
  88.         if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
  89.             throw new IllegalStateException(response.getEntity(String.class));
  90.         }
  91.         GenericType<String> type = new GenericType<String>() {};
  92.         return response.getEntity(type);
  93.     }
  94.  
  95.     private static String delete(Client client,
  96.                                        @NotNull Integer id) {
  97.         WebResource webResource = client.resource(URL);
  98.         if (id != null)
  99.             webResource = webResource.queryParam("id", id.toString());
  100.         ClientResponse response =
  101.                 webResource.accept(MediaType.APPLICATION_JSON)
  102.                         .delete(ClientResponse.class);
  103.         if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
  104.             throw new IllegalStateException(response.getEntity(String.class));
  105.         }
  106.         GenericType<String> type = new GenericType<String>() {};
  107.         return response.getEntity(type);
  108.     }
  109.  
  110.     private static String create(@NotNull Client client,
  111.                                        @NotNull String name,
  112.                                        @NotNull String surname,
  113.                                        @NotNull Integer age,
  114.                                        @NotNull String abbreviation,
  115.                                        @NotNull String job) {
  116.         WebResource webResource = client.resource(URL);
  117.         webResource = webResource.queryParam("name", name);
  118.         webResource = webResource.queryParam("surname", surname);
  119.         webResource = webResource.queryParam("age", age.toString());
  120.         webResource = webResource.queryParam("abbreviation", abbreviation);
  121.         webResource = webResource.queryParam("job", job);
  122.         ClientResponse response =
  123.                 webResource.accept(MediaType.APPLICATION_JSON)
  124.                         .get(ClientResponse.class);
  125.         System.out.println(response.getStatus());
  126.         if (response.getStatus() != ClientResponse.Status.OK.getStatusCode()) {
  127.             throw new IllegalStateException("Request failed");
  128.         }
  129.         GenericType<String> type = new GenericType<String>() {};
  130.         return response.getEntity(type);
  131.     }
  132.     private static void printList(List<Person> persons) {
  133.         for (Person person : persons) {
  134.             System.out.println(person);
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement