Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot RestTemplate post without response type

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?

like image 541
T Anna Avatar asked Jun 22 '18 08:06

T Anna


People also ask

What is difference between getForObject and getForEntity?

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.

What is the difference between postForObject and postForEntity?

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.

How do you use RestTemplate POST method?

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 );


2 Answers

You can use postForLocation instead of postForEntity.

restTemplate.postForLocation(postUrl, request)
like image 176
pvpkiran Avatar answered Sep 16 '22 15:09

pvpkiran


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

like image 34
wutzebaer Avatar answered Sep 17 '22 15:09

wutzebaer