Advertisement
kernel_memory_dump

REST

Dec 14th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import org.apache.http.HttpResponse;
  2. import org.apache.http.client.HttpClient;
  3. import org.apache.http.client.methods.HttpPost;
  4. import org.apache.http.entity.StringEntity;
  5. import org.apache.http.entity.mime.MultipartEntity;
  6. import org.apache.http.entity.mime.content.FileBody;
  7. import org.apache.http.impl.client.HttpClientBuilder;
  8.  
  9. GsonBuilder builder = new GsonBuilder();
  10. Gson gson = builder.create();
  11.  
  12. MojaKlasa mojObjekat = new MojaKlasa(); // Plain Old Java Object (getters+setters+no arg constructor)
  13. String mojObjekatKaoJSON = gson.toJson(mojObjekat );
  14. HttpResponse response = sendJSONPOST("http://localhost:8080/WebApp/urlKaMomRestServisu, mojObjekatKaoJSON);
  15. System.out.println("RESPONSE FROM THE SERVER:\n" + response.toString());
  16.              
  17.  
  18.  
  19.  
  20.     public static HttpResponse sendJSONPOST(String urlString, String data) throws Exception {
  21.         HttpClientBuilder clientBuilder = HttpClientBuilder.create();
  22.         HttpClient client = clientBuilder.build();
  23.         HttpPost post = new HttpPost(urlString);
  24.         StringEntity  postingString =new StringEntity(data);//convert your pojo to   json
  25.         post.setEntity(postingString);
  26.         post.setHeader("Content-type", "application/json");
  27.         HttpResponse  response = client.execute(post);
  28.         return response;
  29.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement