I need to call another api and I am trying to use postForEntity as below:
restTemplate.postForEntity(postUrl, request, responseType)
Now, the api that I am calling is returning an object of a particular type, let's say CustomerDto. But I am not interested in this object. I just want to post and I don't want anything in return so I don't want to create this CustomerDto class in my application. So, for this scenario, in the above statement, what should i replace for responseType. In case, I had wanted the returned object, I would have given CustomerDto.class in case of responseType, but I dont want anything in return, so what do I do?
For example, the method getForObject() will perform a GET and return an object. getForEntity() : executes a GET request and returns an object of ResponseEntity class that contains both the status code and the resource as an object. getForObject() : similar to getForEntity() , but returns the resource directly.
Compared to postForObject(), postForEntity() returns the response as a ResponseEntity object. Other than that, both methods do the same job. Let's say that we want to make a POST request to our Person API to create a new Person object and return the response as a ResponseEntity.
String url = "https://app.example.com/hr/email"; Map<String, String> params = new HashMap<String, String>(); params. put("email", "[email protected]"); RestTemplate restTemplate = new RestTemplate(); ResponseEntity<String> response = restTemplate. postForEntity( url, params, String. class );
You can use postForLocation
instead of postForEntity
.
restTemplate.postForLocation(postUrl, request)
Another way is to use Void.class
as response-type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With