Reputation: 853
I should make a request to the address and send json, json I have in the form of a model
public class RequestToPlanfix {
private String cmd;
private String providerId;
private String channel;
private String chatId;
private String planfix_token;
private String message;
private String title;
private String contactId;
private String contactName;
private String contactLastName;
private String contactIco;
private String contactEmail;
private String contactPhone;
private String contactData;
@Convert(converter = Attachments.class)
private Map<String,String> name;
@Convert(converter = Attachments.class)
private Map<String,String> url;
private Boolean isEcho;
}
that is, I should send json via https whose fields match the fields of this entity, how can I get and translate this entity into json? How can I make an https request further?
then I will receive the answer in the form of json, the fields of which are the fields of the next entity
that is, I will get json and I should do it parse
public class RequestFromPlanfix {
private String cmd;
private String providerId;
private String chatId;
private String contactPhone;
private String channel;
private String token;
private String message;
private String userName;
private String userLastName;
private String userIco;
private String taskEmail;
@Convert(converter = Attachments.class)
private Map<String,String> name;
@Convert(converter = Attachments.class)
private Map<String,String> url;
}
the fields in two cases are different in some places, so I created two entities, then when we send the request we will send the first json, and when we receive the answer the second json, I cannot understand how to make the request and associate the json with the entity, thank you
String postUrl = "www.site.com";
Gson gson = new Gson();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(postUrl);
StringEntity postingString = new StringEntity(gson.toJson(requestToPlanfix));//gson.tojson() converts your pojo to json
post.setEntity(postingString);
post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
Upvotes: 0
Views: 83
Reputation: 1125
So you need to make a HTTP POST
call.
And as it is explained your request
body looks like RequestToPlanfix.java
and your expected Response
will be like RequestFromPlanfix.java
.
Furthermore, you're using GSON
for transformation.
Now I'll advise you to re-define your models (RequestToPlanfix.java
& RequestFromPlanfix.java
) as :
Go to : http://www.jsonschema2pojo.org/
Paste your JSON
request first and choose GSON, Java
and any additional field you think is valid.
response
JSON you're expecting, generate your RequestFromPlanfix.java
.Now , lets make the HTTP call
using Spring's RestTemplate
:
public class MakeRestCall {
@Autowired
private RestTemplate restTemplate;
public void getResponse(){
String postUrl = "www.site.com";
// get your request object ready
RequestToPlanfix requestObject = getRequestObject();
// initialize HttpEntity
HttpEntity<RequestToPlanfix> httpEntity = new HttpEntity<>(requestObject);
ResponseEntity<RequestFromPlanfix> response = restTemplate.exchange(postUrl, HttpMethod.POST, httpEntity, RequestFromPlanfix.class);
RequestFromPlanfix responseObject = response.getBody();
// carry-on with your other stuff
}
Hope this helps.
Upvotes: 1
Reputation: 300
You can use RestTemplate.
Something like:
@Test
public void test() throws URISyntaxException {
RestTemplate restTemplate = new RestTemplate();
final String baseUrl = "https://your_url!";
final URI uri = new URI(baseUrl);
final RequestToPlanfix requestToPlanfix = new RequestToPlanfix();
// set fields here to requestToPlanfix
final ResponseEntity<RequestFromPlanfix> result = restTemplate.postForEntity(uri, requestToPlanfix, String.class);
// your response
final RequestFromPlanfix requestFromPlanfix = result.getBody();
//Verify request succeed
Assert.assertEquals(200, result.getStatusCodeValue());
}
Other examples you can find here
Upvotes: 0