Hi I am trying to call some Soft layer APIs and was able to make simple calls as well as calls which include passing some ids using Spring's RestTemplate in java, but not able to make a similar call in java for below rest URL.
// formatted for readability
https://getInvoices?
objectFilter={
"invoices":{
"createDate":{
"operation":"betweenDate",
"options":[
{
"name":"startDate",
"value":[
"06/01/2016"
]
},
{
"name":"endDate",
"value":[
"06/02/2016"
]
}
]
}
}
}
Can anyone help me out how to do the same in java using springs rest template or even using soft layer rest client.
If you are willing to user Jersey Client API, your code could be like:
String json = "{\"invoices\":{\"createDate\":{\"operation\":\"betweenDate\",\"options\":[{\"name\":\"startDate\",\"value\":[\"06/01/2016\"]},{\"name\":\"endDate\",\"value\":[\"06/02/2016\"]}]}}}";
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://api.softlayer.com")
.path("rest")
.path("v3")
.path("SoftLayer_Account")
.path("getInvoices")
.queryParam("objectFilter",
URLEncoder.encode(json, StandardCharsets.UTF_8.toString()));
String result = target.request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
With Spring RestTemplate, you would do:
String json = "{\"invoices\":{\"createDate\":{\"operation\":\"betweenDate\",\"options\":[{\"name\":\"startDate\",\"value\":[\"06/01/2016\"]},{\"name\":\"endDate\",\"value\":[\"06/02/2016\"]}]}}}";
RestTemplate restTemplate = new RestTemplate();
URI targetUrl = UriComponentsBuilder
.fromUriString("https://api.softlayer.com")
.path("rest")
.path("v3")
.path("SoftLayer_Account")
.path("getInvoices")
.queryParam("objectFilter",
URLEncoder.encode(json, StandardCharsets.UTF_8.toString()))
.build()
.toUri();
String result = restTemplate.getForObject(targetUrl, String.class);