12

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?

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
T Anna
  • 874
  • 5
  • 21
  • 52

2 Answers2

23

You can use postForLocation instead of postForEntity.

restTemplate.postForLocation(postUrl, request)
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • Thanks much for the solution. :) – T Anna Jun 22 '18 at 12:49
  • 4
    I would still appreciate if Spring would add a simple `restTemplate.post(url)` method. There are use cases where the response can simply be ignored. `postForLocation` works fine, but the method name looks awkward, if the return is simply ignored. – Stefan Haberl Mar 05 '20 at 12:11
11

Another way is to use Void.class as response-type

wutzebaer
  • 14,365
  • 19
  • 99
  • 170