user9735824
user9735824

Reputation: 1256

how to pass object on Spring's REST Template using get

I am using a Spring REST template to pull data using POST and everything is working fine.

ResponseEntity<MyObject> resp= restTemplate.postForEntity("url", inputParam, MyObject.class);

But now since I am not doing any POST operation, I want to change it to GET. I can do this by adding all input params as url parameters and do:

ResponseEntity<MyObject> resp= restTemplate.getForEntity("url",MyObject.class);

But the problem is, inputParam has alot of parameters, so preparing the url manually is not the best solution. Also GET requests have length restrictions.

Is there any other better solution for handling this?

Upvotes: 1

Views: 1087

Answers (1)

John Humphreys
John Humphreys

Reputation: 39224

First of all, I think your second line should say getForEntity().

Secondly, there are numerous URL builder class options if you google around (including ones from Spring). So, I would use a URL building class to prepare the URL rather than manually doing it yourself which can get messy.

Length Restriction

There's a good SO entry here noting length restrictions of common browsers; so if its going through a browser then I'd stick to POST if you're potentially over the 2000 lower limit they suggest.

Technically there shouldn't be a limit according to https://www.w3.org/2001/tag/doc/get7#myths.

I think on a lot of back-end technologies there is no limit. So, if this is API-only and not going through a browser (like back-end to back-end) then you may be able to ignore those limits. I'd recommend looking into that specifically though and testing it with your back-end.

UniRest

Also, as a personal recommendation, I have found UniRest to be an amazingly useful REST client which makes most of my code much cleaner :). If you have time, maybe try giving that a shot.

http://unirest.io/java.html

Upvotes: 1

Related Questions