Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.impl.client.HttpClientBuilder;
- GsonBuilder builder = new GsonBuilder();
- Gson gson = builder.create();
- MojaKlasa mojObjekat = new MojaKlasa(); // Plain Old Java Object (getters+setters+no arg constructor)
- String mojObjekatKaoJSON = gson.toJson(mojObjekat );
- HttpResponse response = sendJSONPOST("http://localhost:8080/WebApp/urlKaMomRestServisu, mojObjekatKaoJSON);
- System.out.println("RESPONSE FROM THE SERVER:\n" + response.toString());
- public static HttpResponse sendJSONPOST(String urlString, String data) throws Exception {
- HttpClientBuilder clientBuilder = HttpClientBuilder.create();
- HttpClient client = clientBuilder.build();
- HttpPost post = new HttpPost(urlString);
- StringEntity postingString =new StringEntity(data);//convert your pojo to json
- post.setEntity(postingString);
- post.setHeader("Content-type", "application/json");
- HttpResponse response = client.execute(post);
- return response;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement