Reputation: 9648
Hi I got a rest endpoint xyz.com/test/create which expect content-type application/json and content is
{
"name": "spring boot"
}
There are some other fields as well in body having array.
I am using rest template in a spring rest controller to hit the above endpoint and i also wants to pass data. I am not sure what domain model at the endpoint side is using to map the data in json from client side to server side.
How to use rest template to hit above endpoint with above data and the Content-Type is application-json.
Upvotes: 0
Views: 288
Reputation: 957
In order to create the data you want to send, use a data type such as Map or plain POJO. A map would look something like:
com.google.common.collect.ImmutableMap.of("name","spring boot")
Alternatively a POJO:
public class dataTransferPOJO {
private String name;
//... NoArgsConstructor, getters, setters ...//
}
Sending the Data with RestTemplate:
Make sure you understand Spring Boot's RestTemplate
, and use a POST request to hit the endpoint:
...
dataTransferPOJO payload = new dataTransferPOJO();
HttpEntity<String> entity = new HttpEntity<String>(payload, headers);
HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, dataTransferPOJO.class);
...
Spring Boot will try to serialize the request you are receiving with FasterXML's Jackson, and your endpoint receiver will look like:
@RequestMapping(value = "/rawTask", method = RequestMethod.POST)
public AsyncTask newRawTask(@Valid @RequestBody dataTransferPOJO payload) throws Exception {
return atrr.save(payload);
}
Upvotes: 1